Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using layout_gravity="bottom" to place at bottom of LinearLayout

I would like to place a layout on the bottom of a LinearLayout, but I can't seem to get it to work. I know that I can use RelativeLayout to do this, but I should be able to use LinearLayout, shouldn't I?

EDIT: Actually this is more confusing than I thought. The layout below is simplified. In reality, I'm using fragments on a pre-3.0 device, with the compatibility layer.

I used Hierarchy Viewer to examine what's going on, and found that an android.support.v4.app.NoSaveStateFrameLayout was added to my layout, and that layout has layout_height set to wrap_content. That seems to be what's causing my problem, but I haven't yet figured out how to fix it.

Specifically, why doesn't this work? Shouldn't the layout_gravity place it at the bottom?

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

        ... stuff here ...

        <LinearLayout 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:orientation="horizontal">

            ... more stuff here ...

     </LinearLayout>
</LinearLayout>

BTW, changing layout_height to fill_parent or setting layout_weight don't seem to work either. I just want to better understand what is going on, because clearly I'm missing something important. Thanks.

like image 417
Shawn Lauzon Avatar asked Jul 29 '11 15:07

Shawn Lauzon


2 Answers

First of all nice question.

Android behaves we can say weird in the situation like this.

if you have selected your parent linear layout's orientation horizontal then you can set its child component at bottom by setting its layoug_gravity=bottom. suppose you have added 2 text views in that horizontal linear layout and second textview's layout_gravity is bottom then it will set to bottom but it work like it is set at bottom in other column then the first text view. NOTE : you can set textview's layout_gravity = "left" or "right" when its parent linearlayout is horizontal but you cant see its result.

Oppositely, if you have selected parent linearlayout's orientation vertical then you can set its child component at left or right by using layout_gravity. but the second textview will shown in you can say next row with left or right gravity as you have set. NOTE you can set textview's layout_gravity = "top" or "bottom" when its linear layout is vertical but you can not see its result.

Try to make sample xml design as i have stated above so you get better idea.

Strange but True!!! Try to understand this behavior. :)

like image 68
Ravi Bhatt Avatar answered Oct 02 '22 12:10

Ravi Bhatt


Just add space between what you want at the bottom and all the rest:

        <Space
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
like image 36
Chalikov Avatar answered Oct 02 '22 11:10

Chalikov