Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Layout problem: how to place something on top and bottom?

I want create a layout, with a horizontal LinearLayout(s) on top and bottom, a ListView fill in middle.

How can I define the main.xml.

I tried to create a layout with horizontal LinearLayout on top, TextView on bottom, a ListView fill in middle; is ok. But after I modified the bottom TextView to LinearLayout, the bottom LinearLayout disappear.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    >
    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal"
        >
        <TextView 
            android:textSize="12px" 
            android:text="something here" 
            android:layout_width="50px" 
            android:layout_height="wrap_content"
            /> 
        <TextView 
            android:textSize="12px" 
            android:text="something here" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            />
    </LinearLayout> 

    <LinearLayout 
        android:orientation="vertical" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:gravity="bottom" 
        >
        <ListView 
            android:id="@+id/listbody" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            />
        <LinearLayout 
            android:orientation="horizontal" 
            android:layout_height="wrap_content" 
            android:layout_width="fill_parent" 
            > 
            <TextView 
                android:layout_height="wrap_content" 
                android:layout_width="0dip" 
                android:layout_weight="1" 
                android:textSize="12px" 
                android:text="50%" 
                /> 
            <TextView 
                android:layout_height="wrap_content" 
                android:layout_width="0dip" 
                android:layout_weight="1" 
                android:textSize="12px" 
                android:text="50%" 
                /> 
        </LinearLayout> 
    </LinearLayout> 
</LinearLayout>

Anybody can tell advise? Please help.

like image 600
chow Avatar asked Jan 13 '11 01:01

chow


2 Answers

Try containing the whole set in a RelativeLayout:

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

    <LinearLayout
        android:id="@+id/top_linear_layout_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" >
    </LinearLayout>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/bottom_linear_layout_id"
        android:layout_below="@id/top_linear_layout_id" />

    <LinearLayout
        android:id="@+id/bottom_linear_layout_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >
    </LinearLayout>
</RelativeLayout>

Edited with sizing stuff.

like image 75
Estel Avatar answered Nov 15 '22 21:11

Estel


Here's what you're looking for. Estel's on the right track with the RelativeLayout (although it can be done with a LinearLayout, I think the RelativeLayout approach is cleaner, though) but the layout is out of order Nevermind, check Estel's comment which proved me wrong. :) You should first define your header, align it to the top; next, define your footer, and align it to the bottom; finally, declare your ListView, give it a height of fill_parent, and set it to layout above the footer and below the header:

<?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"
    >
    <LinearLayout
        android:id="@+id/header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:alignParentTop="true"
        >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/footer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:alignParentBottom="true"
        >
    </LinearLayout>

    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@id/footer"
        android:layout_below="@id/header"
        />
</RelativeLayout>
like image 29
Kevin Coppock Avatar answered Nov 15 '22 20:11

Kevin Coppock