How can I align an element to the right in the FrameLayout?

Houcine picture Houcine · May 19, 2011 · Viewed 53.1k times · Source

I have a FrameLayout which contains 2 views , the second is something like a Close Button (X) and i want to position it on the right.

I've tried layout_gravity = " right ", but that didn't work.

This is my layout xml :

<FrameLayout android:layout_width="320dp"
    android:layout_height="wrap_content"
    android:id="@+id/layoutSmallImg">

    <ImageView android:id="@+id/bigImage"
        android:layout_width="320dp" android:layout_height="wrap_content"/>

    <ImageView android:id="@+id/bigImage"
        android:layout_width="320dp" android:layout_height="wrap_content"/>

    <ImageButton android:id="@+id/closebutton"
        android:background="@android:color/transparent"
        android:layout_height="30dp" android:layout_marginTop="10dp"
        android:layout_alignParentRight="true"
        android:layout_width="30dp" android:paddingLeft="40dp"
        android:visibility="gone" 
        android:src="@drawable/close" />
</FrameLayout>

Answer

Aleadam picture Aleadam · May 19, 2011

I recommend you to use a RelativeLayout instead

FrameLayout is designed to block out an area on the screen to display a single item. You can add multiple children to a FrameLayout and control their position within the FrameLayout using gravity. Children are drawn in a stack, with the most recently added child on top. The size of the frame layout is the size of its largest child (plus padding), visible or not (if the FrameLayout's parent permits). Views that are GONE are used for sizing only if setConsiderGoneChildrenWhenMeasuring() is set to true.

BTW, do you want the button on top of the ImageView or next to it? You're setting the width of both the layout and the imageView to the same size.


After the comment, I can see two options: