layout_marginBottom 与 android:layout_margin 一起设置时无效

layout_marginBottom has no effect when set with android:layout_margin

在继续 post 这个问题之前,我在 SO 上尝试了多个答案。 None 其中有帮助。

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
 
       <allosh.xvideo.player.views.PlayerVideoView
            android:layout_centerInParent="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:layout_alignParentTop="true"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="100dp"/>
     </RelativeLayout>
</RelativeLayout>

LinearLayout的android:layout_marginBottom="100dp"没有做任何事情,而android:layout_margin="5dp"有一个可见的效果。
我不仅在寻找解决方案,而且还在寻找适当的解释。这将是有益的和赞赏的。

设置 android:layout_margin 属性会覆盖此处的 android:layout_marginBottom。因此,如果您想要单独的底部边距,则必须分别指定开始、结束和顶部边距。


如果您对技术解释感兴趣,布局参数由 MarginLayoutParams class 阅读。这是构造函数的简化片段:

int margin = a.getDimensionPixelSize(R.styleable.ViewGroup_MarginLayout_layout_margin, -1);
if (margin >= 0) {
    
    leftMargin = margin;
    topMargin = margin;
    rightMargin= margin;
    bottomMargin = margin;
} else {
    
    int horizontalMargin = a.getDimensionPixelSize(R.styleable.ViewGroup_MarginLayout_layout_marginHorizontal, -1);

    ...
}

如您所见,他们首先读取 'all-sides' 边距属性,然后 只有在未设置 时,他们才会继续检查其他边距属性。具体来说,他们按以下顺序检查:

  1. android:layout_margin
  2. android:layout_marginHorizontalandroid:layout_marginVertical
  3. android:layout_marginLeftandroid:layout_marginBottom

你应该:

  1. 设置 marginLeft、marginRight 和 marginTop 而不是 margin
  2. 从 PlayerVideoView
  3. 中删除 layout_alignParentBottom
  4. 为您的 PlayerVideoView 设置 alignBottom
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
 
       <allosh.xvideo.player.views.PlayerVideoView
            android:layout_centerInParent="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignBottom="@+id/linear1"
            android:layout_alignParentTop="true"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

        <LinearLayout
            android:id="@+id/linear1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="100dp"/>
     </RelativeLayout>
</RelativeLayout>