Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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> 
like image 230
Houcine Avatar asked May 19 '11 15:05

Houcine


People also ask

How do I align a view to the bottom in FrameLayout?

Actually it's possible, despite what's being said in other answers. If you have a FrameLayout , and want to position a child item to the bottom, you can use android:layout_gravity="bottom" and that is going to align that child to the bottom of the FrameLayout .

What's a FrameLayout When should you use FrameLayout instead of RelativeLayout?

A common rule of thumb when choosing layouts is to select the combination that results in the smallest number of nested layout views. Specific to your question, RelativeLayout is larger and more capable than the much simpler FrameLayout. So for simple layouts, the latter is probably more efficient.

Why does FrameLayout hold one view?

Frame Layout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other.


2 Answers

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:

  • android:paddingLeft="250dp"

  • make the button size 320dp and add a custom background, mostly transparent. Make it patch 9 so you can determine where to add the text in it (see my answer here for how to do that: How to create android spinner without down triangle on the right side of the widget)

like image 51
Aleadam Avatar answered Oct 07 '22 22:10

Aleadam


Read the DOCS:

FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other. You can, however, add multiple children to a FrameLayout and control their position within the FrameLayout by assigning gravity to each child, using the android:layout_gravity attribute.

Try:

<TextView      .....      android:layout_gravity="right" /> 

You can place a view in 9 different places within a FrameLayout

Enjoy

like image 29
Yotam Avatar answered Oct 07 '22 23:10

Yotam