How to get android:layout_gravity="bottom" to work in LinearLayout

Go To StackoverFlow.com

2

I am trying to have 1 image on the top of my LinearLayout and 1 at the bottom on the LinearLayout.

But after all the things I tried, I can only get 1 image layout below another. I want 1 align to the top, 1 align to the bottom.

<LinearLayout 
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <ImageView 
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:background="@drawable/bg1"
        android:layout_gravity="top" />
    <ImageView 
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:background="@drawable/bg2"
        android:layout_gravity="bottom" />
</LinearLayout>

I have tried using RelativeLayout (in the main container) with layout_alignParentBottom (in my second image), but that does not solve my problem either.

Thank you for any idea.

2012-04-04 20:25
by michael


3

The following RelativeLayout works fine (tested):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="wrap_content"
android:layout_height="match_parent" >

<ImageView
    android:id="@+id/ImageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="@drawable/bg1" />

<ImageView
    android:id="@+id/ImageView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_toRightOf="@+id/ImageView1"
    android:background="@drawable/bg2" />

If you still have problems check the container above this RelativeLayout. If it's the root everything looks fine.

2012-04-04 20:45
by azertiti
Thanks. But with RelativeLaoyout, I can't set orientation to vertical. And is there any reason why layout_gravity="bottom" does not work in my case - michael 2012-04-04 22:22
A LinearLayout lays things out in a linear one after the other manner (as it name implies). I suspect that the second image is at the bottom of the linear layout that's not big enough to fill its parent. RelativeLayout is the way to go - setting vertical orientation is accomplished by aligning to parent left/center/right and aligning to top or bottom. You need to make sure that your height fills the parent (match_parent) so that to is big enough align top/bottom to the screen/parent - Colin 2012-04-05 02:30
As Colin said, there is no need to specify an orientation for the RelativeLayout. The Views are placed inside depending on the way you align them. In this case I put one on top of it and another at the bottom. Because the layout fills all the available space on vertical it will look like a vertical layout with one View at the top and one at the bottom. Just try it ; - azertiti 2012-04-05 06:49
To position an image at the bottom of the screen I just used this solution and nested a LinearLayout for the rest of my content. Kept most of the simplicity of a LinearLayout that way - Nick 2014-03-25 14:18


0

This is the solution to your problem:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ImageView
        android:id="@+id/imgTop"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:background="@drawable/bg1"
        android:layout_alignParentTop="true" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_below="@id/imgTop"
        android:layout_above="@+id/imgBottom">

    </LinearLayout>


    <ImageView
        android:id="@id/imgBottom"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:background="@drawable/bg2"
        android:layout_alignParentBottom="true" />

</RelativeLayout>
2012-04-04 20:48
by waqaslam
Ads