I have a Layout which contains a ListView some开发者_如何转开发 buttons below it.
What I want is for the buttons to be fixed to the bottom of the screen.
When the ListView has several items (more than what fits the screen) the code is working properly and the buttons appear in the bottom of the screen.
However, when the ListView is empty or does not require scrolling, the buttons are coming up the screen (they are not staying fixed in the bottom).
Here's the layout (some details omitted but I can add them if necessary):
<LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView />
<ListView
android:width="fill_parent"
android:height="wrap_parent"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|bottom"
android:orientation="horizontal"
android:layout_weight="0" />
<Button
android:layout_weight="1" />
<Button
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
I expected setting the "gravity" of the second LinearLayout to "bottom" to make the buttons fixed to the bottom of the screen, but it's not working in the mentioned case.
Any ideas?
Thanks.
You have the layout_height of your top LinearLayout set to wrap_content. This means that it will shrink as its contents shrink. Try setting it to fill_parent instead. Or, give it a layout_weight of 1.
One of the options is to create TextView which will be shown instead of listview, when it is empty.
You do it with listView.setEmptyView(emptyView)
Then set layout_weight of this TextView to same weight as your listview. So when listview is empty, its place will be taken by emptyView and buttons will stay fixed at the bottom.
Also set your emptyView to android:gravity="center"
.
Other option:
- In vertical RelativeLayout add to the layout with buttons property android:layout_alignParentBottom="true".
- All the stuff with listView put inside LinearLayout and add to it android:layout_above="@+id/buttons_layout". That's it. Elements inside LinearLayout will take their place depending on weight but whole layout wont go beyond buttons.
精彩评论