Problems understanding Android XML layout_weight Problems understanding Android XML layout_weight xml xml

Problems understanding Android XML layout_weight


layout_weight is used to determine how Android divides up any left over space after the various widgets get all of the space they want.

So say you have 3 widgets in a row, each of which want 10 pixels, but you have 50 pixels worth of space. Here are a few examples of layout_weights and the number of pixels each widget will claim:

widget1: weight = 1, 30pxwidget2: weight = 0, 10pxwidget3: weight = 0, 10pxwidget1: weight = 1, 20pxwidget2: weight = 1, 20pxwidget3: weight = 0, 10pxwidget1: weight = 1, 16.5pxwidget2: weight = 1, 16.5pxwidget3: weight = 1, 16.5px

You can think of the weight as a percentage of available space. It will add up all of the values, and then give out portions as appropriate. Thus, if it helps you can use weights that add up to 100 to do percentages:

widget1: weight = 0, 10pxwidget2: weight = 25, 15pxwidget3: weight = 75, 25px

If I understand correctly, you want the bottom widget to start at a particular size, then expand to some maximum size if necessary, and then scroll.

And, you want the widget above it to take over all of the extra space.

Unfortunately in Android its not realy possible to specify a maximum size. I think the best you could do would be to give the ScrollView some fixed size, with a layout_weight = 1 and tell the bottom LinearLayout to wrap_content. This would (I think) cause the LinearLayout to expand until it can't anymore, because the ScrollView has already claimed the space.

However, the challange is specifying a fixed size for the ScrollView that makes sense at all different screen sizes and resolutions. You might end up having to create different layouts for the different resolutions, which is probably not worth it. Personally, I'd just give the editText a fixed size...


I've had a similar problem (a footer with the refresh timestamp sticking to the bottom of the screen and a ListView/ScrollView taking up all the remainig space) and after many tries it worked for me using the following layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"             android:layout_width="match_parent"             android:layout_height="match_parent"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"              android:layout_width="fill_parent"              android:layout_height="fill_parent"               android:layout_above="@+id/refresh_date_text"               android:layout_alignParentTop="true"              android:orientation="vertical"               >    <ListView android:id="@android:id/list" android:layout_width="fill_parent"        android:layout_height="fill_parent" android:background="#FFFFFF"        android:layout_weight="1" android:divider="@drawable/list_divider"        android:dividerHeight="1dp" android:drawSelectorOnTop="false" />    <TextView android:id="@android:id/empty"              android:layout_width="match_parent"              android:layout_height="match_parent"              android:background="#ffffff"              android:gravity="center_horizontal"              android:paddingTop="10dp"              android:text="The list is empty."/></LinearLayout><TextView android:id="@+id/refresh_date_text"          android:layout_width="match_parent"           android:layout_height="wrap_content"           android:layout_alignParentBottom="true"          android:gravity="center_horizontal"           android:background="#f0f0f0"           android:textStyle="italic"           android:textSize="12dp"          /></RelativeLayout>

No clipping the ListView when with a scroller, perfect alignment in all orientations, properly showing the "list is empty" text.


I believe you would be better off with a RelativeLayout. Add your header, and set it to layout_alignParentTop="true". Then add your EditText and Button, set that area to layout_alignParentBottom="true". Then, add the center area, set the width and height to fill_parent, and set layout_above="{the id of the EditText/Button layout}", and layout_below="{the id of the header layout}".

I don't have Eclipse at work, otherwise I would test it for you, but that should work.