为什么marginBottom不工作?

fis*_*h40 41 xml android android-layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/messageLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/messageSender"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"/>

    <TextView
        android:id="@+id/messageSenderName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/messageSender"
        android:ellipsize="end"
        android:maxLines="1"
        android:singleLine="false"
        android:textColor="@color/list_text_color"
        android:textSize="16dp"
        android:layout_marginTop="5dp" />

    <TextView
        android:id="@+id/messageContent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/messageSender"
        android:layout_below="@id/messageSenderName"
        android:ellipsize="end"
        android:maxLines="1"
        android:singleLine="false"
        android:textColor="@color/codeFont"
        android:textSize="13dp"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

在我的布局中,我有问题.当我设置marginTop="5dp"它很好,但是当我使用marginBottom时,我的布局没有任何反应.当我在RelativeLayout其中设置填充时也不起作用.这里有什么问题?你能给我任何解决方案吗?

Par*_*ani 104

marginBottom如果你设置android:layout_height="wrap_content"为无效<RelativeLayout>,而是将其设置为match_parent并检查.

  • 没有必要更新..有意义..许多ans可以更新吗? (9认同)
  • @RosePerrone我知道,但我认为这个答案是2年前发布的;) (4认同)
  • 似乎已在API 19+中修复. (2认同)

Seb*_*tes 19

它实际上只是一个bug <RelativeLayout>,所以你可以尝试将RelativeLayout包装在一个内部<LinearLayout>并在那里设置边距.


小智 13

我不确定你在哪个版本的android中遇到这个问题.我查看了Android 4.2.2中的RelativeLayout(https://android.googlesource.com/platform/frameworks/base/+/android-4.2.2_r1.1/core/java/android/widget/RelativeLayout.java)和我相信onMeasure中有一个错误.

在第486行至第488行中,我们有以下代码:

if (isWrapContentHeight) {
    height = Math.max(height, params.mBottom);
}
Run Code Online (Sandbox Code Playgroud)

在我看来,上面应该是:

if (isWrapContentHeight) {
    height = Math.max(height, params.mBottom + params.bottomMargin);
}
Run Code Online (Sandbox Code Playgroud)

使用android:layout_height ="wrap_content"时,RelativeLayout不会出现考虑最后一个垂直布局视图的bottomMargin.

我建议你尝试下列之一:

*)添加一个虚拟视图,它将是最后一个垂直布局的视图,并确保它具有android:layout_below属性.请注意,虚拟视图没有底部边距,因此RelativeLayout会考虑在其上方布置的底部视图边距.对于您的情况,虚拟视图将如下所示:

<View
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_below="@id/messageSender"
/>
Run Code Online (Sandbox Code Playgroud)

*)正如其他人所提到的,在其他方面达到相同的效果,例如Padding.

*)一个不那么重要的解决方案是将RelativeLayout,其平台样式定义和池管理类引入您的项目,执行我上面提到的错误修复,并在通常使用RelativeLayout的任何地方使用它.

*)向Google提交错误

希望这可以帮助.


lu *_*uan 10

也许你可以设置android:paddingBottom=""<RelativeLayout>,以达到同样的效果.

  • 对于其他难以理解的疑虑,这是一个可以接受的解决方法。 (2认同)