簡體   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