meh
meh

Reputation: 22479

android:layout_marginRight attribute not working on RelativeLayout

I have an Inner relative layout with 2 images, and when I set the android:layout_marginRight attribute it doesn't do anything.

Here is my code:

<RelativeLayout
    android:id="@+id/location_image_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="80dp" />

    <ImageView
        android:layout_width="69.33dp"
        android:layout_height="72dp"
        android:background="@drawable/icon"
        android:layout_centerInParent="true" />

</RelativeLayout>

Upvotes: 7

Views: 11824

Answers (5)

Arvind Kanjariya
Arvind Kanjariya

Reputation: 2097

Try to use LinerarLayout instead of RelativeLayout for applying Margin in your Layout.

Upvotes: -5

roman on the way
roman on the way

Reputation: 11

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    ...
/>

I encounter the same problem and if you change the parent RelativeLayout's layout_width to "match_parent" it works. Hope it works for you too.

Upvotes: 1

mokeyking
mokeyking

Reputation: 256

The problem is in the layout's property android:layout_width. When it is set to "wrap_content",the android:layout_marginRight won't working, but rather, only when it is set to "fill_parent", the android:layout_marginRight will work.

Upvotes: 24

MKB
MKB

Reputation: 7619

<LinearLayout
    android:id="@+id/location_image_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="right"
    android:orientation="vertical" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="80dp" />

    <ImageView
        android:layout_width="69.33dp"
        android:layout_height="72dp"
        android:background="@drawable/icone"
        android:layout_centerInParent="true" />

</LinearLayout>

Try this. It work for me and exact that you want

Upvotes: 1

Arpit Patel
Arpit Patel

Reputation: 1571

You have used the following thats why it is happening

android:layout_alignParentRight="true"

just remove this line you will get the margin from the right in your imageview..

Upvotes: 1

Related Questions