【发布时间】:2011-06-19 16:48:33
【问题描述】:
我有一个带有项目的可滚动 ListView(例如在 http://developer.android.com/resources/tutorials/views/hello-listview.html 中)。我对项目使用ArrayAdapter,并将其用作setListAdapter 中的参数。现在我想在屏幕底部添加一个不随列表滚动的按钮。有人可以给我一些提示或发布代码 sn-p 怎么可能完成?
【问题讨论】:
我有一个带有项目的可滚动 ListView(例如在 http://developer.android.com/resources/tutorials/views/hello-listview.html 中)。我对项目使用ArrayAdapter,并将其用作setListAdapter 中的参数。现在我想在屏幕底部添加一个不随列表滚动的按钮。有人可以给我一些提示或发布代码 sn-p 怎么可能完成?
【问题讨论】:
如果你的活动扩展了 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>
请注意,列表视图的 layout_weight 设置为 1。这将使按钮固定在底部的位置。希望有帮助。祝你好运!
【讨论】:
weight 并且 orientation 是 vertical ,那么您应该有:android:layout_height="0dp"
android:layout_marginTop="20px" 添加到我的按钮后,ListView 不再显示,并且按钮位于屏幕顶部。我删除了marginTop,但它的行为仍然如此。有没有一种安全的方法可以在 Button 和 ListView 之间获得一个很小的空间?我这样做对吗,还是我的代码应该受到责备?
您可以使用 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 并使用getListView() 来获取它来生成ListView。但是如果我现在使用一个布局并使用setContentView 来设置它,应用程序会抛出以下异常:Your content must have a ListView whose id attribute is 'android.R.id.list'
我的解决方案基于 Houcline 的 solution 但 ListView 始终高于 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"/>
【讨论】: