private static
private static

Reputation: 770

layout_marginBottom has no effect when set with android:layout_margin

Before proceeding to post this question, I tried multiple answers on SO. None of which were helpful at all.

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

The LinearLayout's android:layout_marginBottom="100dp" does not do anything, while android:layout_margin="5dp" has a visible effect.
I'm not only looking for a solution, but also for a proper explanation. It will be beneficial and appreciated.

Upvotes: 1

Views: 506

Answers (2)

AliSh
AliSh

Reputation: 10619

You should:

  1. Set marginLeft, marginRight and marginTop instead of margin
  2. Remove layout_alignParentBottom from PlayerVideoView
  3. Set alignBottom for your PlayerVideoView
<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>

Upvotes: 1

Henry Twist
Henry Twist

Reputation: 5980

Setting the android:layout_margin attribute overrides android:layout_marginBottom here. So if you want a separate bottom margin, you will have to specify start, end and top margins individually.


If you're interested in a technical explanation, the layout parameters are read by the MarginLayoutParams class. This is a simplified snippet from the constructor:

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);

    ...
}

As you can see, they read the 'all-sides' margin attribute first and then only if this is unset do they move on to checking the other margin attributes. To be specific, they check in this order:

  1. android:layout_margin
  2. android:layout_marginHorizontal, android:layout_marginVertical
  3. android:layout_marginLeft, android:layout_marginBottom etc.

Upvotes: 1

Related Questions