How to configure layout (height and width) and layout containers?

I created a TableLayout with two rows, where the first row has one TextView , and the second row has two columns, the first has ListView , and the second has one EditText and Button each in different rows. I want both rows to be the same height (exactly half the screen), and both columns of the second row should be the same width (exactly half the screen). I want to build something like this: enter image description here

How do I proceed? Which layout should I choose?

+1
android android-layout android-widget
May 28 '12 at 5:38
source share
1 answer

I do not think your current solution will work (due to the requirement for equal space (width and height) + the presence of a ListView ). One solution is to use nested weights (which are bad for performance, but (perhaps not knowing what you are doing) is not something important that will break your application), as in the layout below:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <include android:layout_width="fill_parent" android:layout_height="wrap_content" layout="@layout/identical_layout" /> <TextView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" android:text="TextView" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" > <ListView android:id="@+id/list" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" > </ListView> <LinearLayout android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:orientation="vertical" > <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="ddd" /> </LinearLayout> </LinearLayout> </LinearLayout> 

Another way to avoid the weights nested problem is to use a RelativeLayout as shown below (I haven't tested it):

 <?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" > <include android:id="@+id/included_layout" android:layout_width="fill_parent" android:layout_height="wrap_content" layout="@layout/identical_layout" /> <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_below="@id/included_layout" > <View android:id="@+id/anchor" android:layout_width="0dp" android:layout_height="0dp" android:layout_centerVertical="true" /> <TextView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_above="@id/anchor" android:layout_alignParentTop="true" android:background="#99cc00" android:text="TextView" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="0dp" android:layout_alignParentBottom="true" android:layout_below="@id/anchor" > <ListView android:id="@+id/list" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" > </ListView> <LinearLayout android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:orientation="vertical" > <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="ddd" /> </LinearLayout> </LinearLayout> </RelativeLayout> </RelativeLayout> 

The identical_layout layout file represents a common layout common to your Activities .

+3
May 28 '12 at 8:14
source share



All Articles