Android 固定按钮下方的滚动列表视图

Android 固定按钮下方的滚动列表视图,android,listview,Android,Listview,我有一个带有项目的可滚动列表视图(如中)。我正在为项目使用ArrayAdapter,并将其用作setListAdapter中的参数。现在我想在屏幕底部添加一个按钮,它不会随列表滚动。有人能给我一些提示或发布一个代码片段来说明如何完成吗?您可以使用RelativeLayout来修复布局底部的按钮,并在上面添加listView,如下所示: <RelativeLayout android:layout_width="match_parent" android:layout_he

我有一个带有项目的可滚动列表视图(如中)。我正在为项目使用
ArrayAdapter
,并将其用作
setListAdapter
中的参数。现在我想在屏幕底部添加一个按钮,它不会随列表滚动。有人能给我一些提示或发布一个代码片段来说明如何完成吗?

您可以使用RelativeLayout来修复布局底部的按钮,并在上面添加listView,如下所示:

<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>

如果您的活动扩展了ListActivity,那么您需要以下内容:

<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>


请注意,listview的布局权重设置为1。这将使按钮固定在底部的位置。希望有帮助。祝你好运

我的解决方案基于Houcline,但
列表视图
始终位于
按钮上方



我通过
ListActivity
扩展类,并使用
getListView()
获得它,从而生成了ListView。但是,如果我现在使用布局并使用
setContentView
进行设置,应用程序会引发以下异常:
您的内容必须有一个id属性为“android.R.id.list”的ListView
我想您应该在获得它时自定义ListView!!你能添加你的代码和日志吗?如果你想使用getListView()很好的解决方案,请将你的列表命名为android:id=“@android:id/list”,但是ListView将位于底部,而包含的项目很少。这不起作用。如果列表已满,则如果您使用的是
weight
,并且
方向是
垂直的,则该按钮将保留在屏幕下方,因此您应该有:
android:layout\u height=“0dp”
Houcine,您是对的。在最近的一次更新中,Google更改了ADT,因此在使用“fill_parent”时会发出警告。自2011年6月我回答这个问题以来,情况发生了很大变化。我更改了答案并将其更新。在我的按钮中添加了
android:layout_marginTop=“20px”
后,列表视图不再显示,按钮位于屏幕顶部。我删除了
marginTop
,但它的行为仍然是这样的。有没有安全的方法在按钮和ListView之间留出一个小空间?我这样做是对的,还是应该怪我的代码?这只适用于布局上有匹配父项的情况…不适用于底部有静态按钮的包装对话框的包装解决方案。
<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"/>