【问题标题】:layout_marginBottom has no effect when set with android:layout_margin使用 android:layout_margin 设置时 layout_marginBottom 无效
【发布时间】:2021-04-28 22:28:43
【问题描述】:

在继续发布此问题之前,我在 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 xml android-layout


    【解决方案1】:

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


    如果您对技术解释感兴趣,可以通过MarginLayoutParams 类读取布局参数。这是构造函数的简化 sn-p:

    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_marginLeftandroid:layout_marginBottom

    【讨论】:

    • 感谢您的努力和解释。这完全有道理。我真的尝试从这个布局中删除每个边距属性,除了导致问题的那个!
    【解决方案2】:

    你应该:

    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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-09
      • 2019-03-15
      • 2012-05-10
      • 2016-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多