The problem with layouts: how to place something on top and bottom?

I want to create a layout with a horizontal LinearLayout at the top and bottom, fill the ListView in the middle.

How to determine main.xml.

I tried to create a layout with a horizontal LinearLayout on top, a TextView at the bottom, filling a ListView in the middle; this is normal. But after I changed the bottom TextView to LinearLayout, the bottom LinearLayout will 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> 

Can anyone say some advice? Please, help.

+6
android layout android-linearlayout
source share
5 answers

Try to include the whole set in 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 by other things.

+11
source share

Here is what you are looking for. Estelle is on the right track with RelativeLayout (although it can be done using LinearLayout, I think the RelativeLayout approach is cleaner, though) , but the layout doesn’t work Nevermind, check Estela’s comment, which proved to me wrong. :) First you must determine your title, align it at the top; then define the footer and align it to the bottom; finally declare your ListView, give it the height of fill_parent and set it to the layout above the footer and under the heading:

 <?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> 
+3
source share

The problem is that you define both of them relative to the size of the content that they contain, but if the combined content of both is larger than the screen, then one of them will have to overlap the other, you'd better define layout_height using an absolute dimension in a linear layout

0
source share

Thanks to everyone for the advice and guidance of RelativeLayout. I can fix it now.

I need to define a LinearLayout footer before the ListView and define the 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> 
0
source share

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> 
0
source share

All Articles