Reputation: 5896
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/messageLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/messageSender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"/>
<TextView
android:id="@+id/messageSenderName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/messageSender"
android:ellipsize="end"
android:maxLines="1"
android:singleLine="false"
android:textColor="@color/list_text_color"
android:textSize="16dp"
android:layout_marginTop="5dp" />
<TextView
android:id="@+id/messageContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/messageSender"
android:layout_below="@id/messageSenderName"
android:ellipsize="end"
android:maxLines="1"
android:singleLine="false"
android:textColor="@color/codeFont"
android:textSize="13dp"/>
</RelativeLayout>
In my layout I have problem. When I set marginTop="5dp"
it's fine, but when I use marginBottom
nothing happens in my layout. Also when I set padding in RelativeLayout
it does not work either. What is the problem here? Could you give me any solution?
Upvotes: 50
Views: 52881
Reputation: 417
In some case you have lots of attributs or you just missed that you included earlier :
android:layout_margin="xdp"
You can't use it with specific layout margination
Upvotes: 0
Reputation: 2380
I had a <View>
inside a <RelativeLayout>
and the marginBottom
on that view did not work. I found out that my chain of layout_below
was broken, so therefore it makes sense that the layout did not know from which view the margin should be calculated.
If you chain it properly using layout_below
or the other positioning tools, you do not need to worry about wrap_content
or match_parent
like others are suggesting. Example:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:id="@+id/separator"
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_marginBottom="11dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/separator">
...
Upvotes: 0
Reputation: 128428
marginBottom
has no effect if you set android:layout_height="wrap_content"
for <RelativeLayout>
, instead set it as match_parent and check.
Upvotes: 116
Reputation: 2938
Make sure the layout_height
of your root container is set to match_parent
instead of wrap_content
.
Upvotes: 1
Reputation: 73
It works if you use android:layout_marginBottom with android:layout_alignParentBottom="true"
.
For example :
android:layout_alignParentBottom="true"
android:layout_marginBottom="50dp">
Upvotes: -2
Reputation: 179
I am not sure in which revision of android you are experiencing this issue. I looked at RelativeLayout in Android 4.2.2 (https://android.googlesource.com/platform/frameworks/base/+/android-4.2.2_r1.1/core/java/android/widget/RelativeLayout.java) and I believe there is a bug in the onMeasure.
In lines 486 to 488, we have following code:
if (isWrapContentHeight) {
height = Math.max(height, params.mBottom);
}
In my opinion, the above should read:
if (isWrapContentHeight) {
height = Math.max(height, params.mBottom + params.bottomMargin);
}
With android:layout_height="wrap_content", RelativeLayout does not appear take into account the bottomMargin of the last vertically laid out view.
I suggest you try one of the following:
*) Add a dummy view that will be the very last vertically laid out view, and ensure that it has the android:layout_below attribute. Note that the dummy view does not have a bottom margin, it is there so that RelativeLayout considers the bottom margin of views laid out above it. For your case, the dummy view would look like this:
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="@id/messageSender"
/>
*) As others have mentioned, achieve the same effect in other ways, such as Padding.
*) A not-so-trivial solution is bring in RelativeLayout, its platform style definitions and Pool management classes into your project, perform the bug fix I mentioned above and use this anywhere you would normally use RelativeLayout.
*) File a bug with Google
Hope this helps.
Upvotes: 14
Reputation: 7227
Maybe you can set android:paddingBottom=""
of <RelativeLayout>
to get the same effect.
Upvotes: 10
Reputation: 512
It is realy just a bug of the <RelativeLayout>
, so you could try to wrap the RelativeLayout inside of a <LinearLayout>
and set the margin there.
Upvotes: 19