繁体   English   中英

使用 android:layout_margin 设置时 layout_marginBottom 无效

[英]layout_marginBottom has no effect when set with android:layout_margin

在继续发布这个问题之前,我在 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 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_marginHorizontal , android:layout_marginVertical
  3. android:layout_marginLeft , android:layout_marginBottom等。

你应该:

  1. 设置 marginLeft、marginRight 和 marginTop 而不是 margin
  2. 从 PlayerVideoView 中删除 layout_alignParentBottom
  3. 为您的 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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM