问题描述
在继续发布这个问题之前,我在 SO 上尝试了多个答案。这些都没有帮助。
<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
类读取布局参数。这是构造函数的简化片段:
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);
...
}
如您所见,他们首先读取“所有边”边距属性,然后只有在未设置时才会继续检查其他边距属性。具体来说,他们按以下顺序检查:
android:layout_margin
-
android:layout_marginHorizontal
,android:layout_marginVertical
-
android:layout_marginLeft
、android:layout_marginBottom
等
你应该:
- 设置 marginLeft、marginRight 和 marginTop 代替边距
- 从 PlayerVideoView 中移除 layout_alignParentBottom
- 为您的 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>