Android ListView: Header/Footer that fills the remaining space on screen
I want to make a chat-like view using ListView where the new messages appear at the bottom of the screen (therefore I use android:stackFromBottom="true") and I want to add a header to the ListView which will appear at the top of the screen. When there are few messages there will be an empty space between the header and the messages, when there a lot of messages, the ListView will be scrollable and the header will be hidden, when the bottom messages are visible (the header will scroll along with the rest of the messages). The problem is that I can't tell the ListView to let it's headers or items fill the remaining space on the screen. android:layout_height="fill_parent" doesn't he开发者_运维百科lp. I can make the header static using RelativeLayout, but I don't want to. Any ideas?
Why don't you just put the list view in a table layout with 3 rows. Put the listview in the middle row then use the top and bottom row as the header and footer.
Use a LinearLayout on vertical with the header at top, your ListView in the middle with layout_weight="1" and then your footer.
One solution if you want the Header to be scrollable. Is to inflate the position 1 in your adapter being the header you want to scroll.
Just make a different layout such as header_list.xml
<?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="wrap_content"
android:background="@drawable/listitem_background"
android:layout_marginLeft="4dip" android:layout_marginRight="4dip"
android:gravity="center_vertical|center_horizontal"
>
<TextView android:id="@+id/nearByLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Load More Results"
android:textColor="#000"
android:textStyle="bold"
android:textSize="20sp"
/>
</LinearLayout>
And in your activity add it like
View headerView = inflater.inflate(R.layout.header_list, null);
yourlistView.addHeaderView(headerView);
精彩评论