【问题标题】:why marginBottom not working?为什么 marginBottom 不起作用?
【发布时间】:2020-04-23 22:53:59
【问题描述】:
<?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>

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

【问题讨论】:

    标签: android xml android-layout


    【解决方案1】:

    如果你为&lt;RelativeLayout&gt;设置了android:layout_height="wrap_content"

    marginBottom无效,而是将其设置为ma​​tch_parent并检查。

    【讨论】:

    • fill_parent 从 API 级别 8 开始被弃用,取而代之的是 match_parent
    • @RosePerrone 是的,我知道,但我认为这个答案是 2 年前发布的;)
    • 不需要更新..有意义..那么可以更新多少个?
    • 似乎在 API 19+ 中已修复。
    【解决方案2】:

    这实际上只是&lt;RelativeLayout&gt; 的一个错误,因此您可以尝试将RelativeLayout 包装在&lt;LinearLayout&gt; 中并在那里设置边距。

    【讨论】:

    • 如果可能的话,只需更改线性的相对值。
    【解决方案3】:

    我不确定您在哪个版本的 android 中遇到此问题。我查看了 Android 4.2.2 (https://android.googlesource.com/platform/frameworks/base/+/android-4.2.2_r1.1/core/java/android/widget/RelativeLayout.java) 中的 RelativeLayout,我认为 onMeasure 中存在错误。

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

    if (isWrapContentHeight) {
        height = Math.max(height, params.mBottom);
    }
    

    在我看来,上面应该是:

    if (isWrapContentHeight) {
        height = Math.max(height, params.mBottom + params.bottomMargin);
    }
    

    使用 android:layout_height="wrap_content",RelativeLayout 不会出现考虑到最后一个垂直布局视图的底部边缘。

    我建议您尝试以下方法之一:

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

    <View
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_below="@id/messageSender"
    />
    

    *) 就像其他人提到的,用其他方式达到同样的效果,比如Padding。

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

    *) 向 Google 提交错误

    希望这会有所帮助。

    【讨论】:

    • 这已在RelativeLayout 的最新版本中得到修复,但现在取决于您使用的目标 SDK 版本。在Android 5.1.1 中,通过检查版本来完成,如下所示:if (targetSdkVersion &lt; Build.VERSION_CODES.KITKAT) { height = Math.max(height, params.mBottom); } else { height = Math.max(height, params.mBottom + params.bottomMargin); },请参见此处的第 502 - 508 行:android.googlesource.com/platform/frameworks/base/+/…
    【解决方案4】:

    也许您可以设置android:paddingBottom=""&lt;RelativeLayout&gt; 以获得相同的效果。

    【讨论】:

    • 这并没有提供问题的答案。要批评或要求作者澄清,请在其帖子下方发表评论。
    • @Naveen 我不知道:)
    • 这是一个可以接受的解决方法,以解决其他无法理解的疑虑。
    【解决方案5】:

    确保您的根容器的layout_height 设置为match_parent 而不是wrap_content

    【讨论】:

    • 当它必须是 wrap_content 时,我怎么可能将其更改为 match_parent - 这在布局的其余部分中是一个巨大的差异!!!!
    【解决方案6】:

    我在&lt;RelativeLayout&gt; 中有一个&lt;View&gt;,而该视图上的marginBottom 不起作用。我发现我的 layout_below 链已损坏,因此布局不知道应该从哪个视图计算边距是有道理的。

    如果您使用layout_below 或其他定位工具正确链接它,则无需像其他人建议的那样担心wrap_contentmatch_parent。示例:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <View
            android:id="@+id/separator"
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:layout_marginBottom="11dp" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/separator">
    
            ...
    

    【讨论】:

      【解决方案7】:

      在某些情况下,您有很多属性,或者您只是错过了之前包含的属性:

      android:layout_margin="xdp"
      

      您不能将其与特定的布局边距一起使用

      【讨论】:

        【解决方案8】:

        如果你使用android:layout_marginBottom with android:layout_alignParentBottom="true",它就可以工作。

        例如:

        android:layout_alignParentBottom="true"
        android:layout_marginBottom="50dp">
        

        【讨论】:

        • 不,不是,问题是 layout_height="wrap_content"
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-07
        • 2019-08-06
        • 2016-07-05
        • 2011-08-06
        相关资源
        最近更新 更多