【问题标题】:Why does ImageView get smaller by increasing margin top size?为什么 ImageView 通过增加边距顶部尺寸变小?
【发布时间】:2018-07-31 08:46:01
【问题描述】:

我有一个相对布局,我想在其中显示一个 Imageview 和一个 TextViewbackground-image

如果我没有使用任何margin-top,它会按我应该的方式工作,但如果我增加 ImageView 的上边距,图像会变小。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout                                                                xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/playerNameEnd"
        android:layout_width="200dp"
        android:layout_height="80dp"
        android:background="@drawable/casino2"
        android:gravity="center"
        android:text="Player"
        android:textColor="@color/cream"
        android:textSize="26sp" />

    <ImageView
        android:id="@+id/roulette_EndPic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        app:srcCompat="@drawable/casino" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout>

【问题讨论】:

  • 如果您没有从中受益,为什么还要使用ConstraintLayout?由于您的 ImageView 的宽度和高度是 wrap_content 并且您的根布局不是 ScrollView,因此它会尝试将所有内容都放在屏幕上,并且当您添加边距时,图像会变小。请注意,ImageView 周围可能有填充
  • 您希望看到什么?如果添加上边距,那么用于绘制图像的空间就会减少,对吧?
  • 去掉约束布局部分,从相对布局开始。

标签: java android android-layout margin android-relativelayout


【解决方案1】:

使用android:paddingTop="40dp"

而不是marginTop

【讨论】:

  • 感谢您的回答,但这是同样的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-17
  • 2015-01-27
  • 2021-01-23
  • 1970-01-01
  • 2019-06-24
相关资源
最近更新 更多