Home » Android » android – Layout problem: how to place something on top and bottom?

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

Posted by: admin June 15, 2020 Leave a comment

Questions:

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.

How to&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.

Answer:

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>

Answer:

The problem would be that you are defining both of them relative to the size of the content they contain, but if the combined content of both is bigger than the screen then one of them will have to overlap the other one, you would be better to define the layout_height using an absolute measurement in a linear layout

Answer:

Thanks all for your advice and direction of RelativeLayout. I can fix it now.

I have to define the footer LinearLayout before the ListView, and define ListView as android:layout_alignParentTop=”true” and android:layout_above=”@id/footer”

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

            <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>
        <ListView
            android:id="@+id/tablebody"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_above="@id/footer"
        />
    </RelativeLayout>
</LinearLayout>

Answer:

This will definitely solve your problem:

<?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">
    <HorizontalScrollView
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:orientation="horizontal">
            <TextView 
                android:textSize="12sp" 
                android:text="something here" 
                android:layout_width="50dp" 
                android:layout_height="wrap_content"/> 
            <TextView 
                android:textSize="12sp" 
                android:text="something here" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content"/>
        </LinearLayout>
    </HorizontalScrollView>
    <ListView 
        android:id="@+id/listbody" 
        android:layout_width="fill_parent" 
        android:layout_height="0dp"
        android:layout_weight="1"/>
    <HorizontalScrollView
        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="12sp" 
                android:text="50%"/> 
            <TextView 
                android:layout_height="wrap_content" 
                android:layout_width="0dip" 
                android:layout_weight="1" 
                android:textSize="12sp" 
                android:text="50%"/> 
        </LinearLayout>
    </HorizontalScrollView>
</LinearLayout>