开发者

Fixed Button below a scrollable ListView

I have a scrollable ListView with items (like in http://developer.android.com/resources/tutorials/views/hello-listview.html). I am using an ArrayAdapter for the items and use it as a parameter in setListAdapter. Now I would like to add a button at the bottom of the screen, which does not scroll with the list. Could someone give me some hin开发者_Python百科ts or post a code snippet how it could possibly be done?


If your activity extends ListActivity then you need something like this:

<LinearLayout android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <ListView android:id="@android:id/list"
              android:layout_height="0dip"
              android:layout_width="match_parent"
              android:layout_weight="1" />

    <Button android:id="@+id/btn" 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

</LinearLayout>

Notice that the listview has a layout_weight set to 1. That will keep the button fixed in its place at the bottom.


you can use a RelativeLayout to fix the button at the bottom of your layout , and add your listView above it like this :

<RelativeLayout android:layout_width="match_parent"
         android:layout_height="match_parent">

      <Button android:id="@+id/btn" 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"/>

     <ListView 
            android:id="@android:id/list"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
               android:layout_above="@id/btn" />
</RelativeLayout>


My solution based on Houcline's solution but ListView always above Button

<CheckBox
    android:id="@+id/chbRemoveIfUninstall"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"/>
<ListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_above="@id/chbRemoveIfUninstall"/>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜