【问题标题】:Fixed Button below a scrollable ListView可滚动 ListView 下方的固定按钮
【发布时间】:2011-06-19 16:48:33
【问题描述】:

我有一个带有项目的可滚动 ListView(例如在 http://developer.android.com/resources/tutorials/views/hello-listview.html 中)。我对项目使用ArrayAdapter,并将其用作setListAdapter 中的参数。现在我想在屏幕底部添加一个不随列表滚动的按钮。有人可以给我一些提示或发布代码 sn-p 怎么可能完成?

【问题讨论】:

    标签: android listview


    【解决方案1】:

    如果你的活动扩展了 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 并且 orientationvertical ,那么您应该有:android:layout_height="0dp"
    • Houcine,你是对的。在最近的一次更新中,Google 更改了 ADT,因此在使用“fill_parent”时会发出警告。自从我在 2011 年 6 月回答这个问题以来,发生了很多变化。我改变了我的答案并使其成为最新的。
    • android:layout_marginTop="20px" 添加到我的按钮后,ListView 不再显示,并且按钮位于屏幕顶部。我删除了marginTop,但它的行为仍然如此。有没有一种安全的方法可以在 Button 和 ListView 之间获得一个很小的空间?我这样做对吗,还是我的代码应该受到责备?
    • 这只有在布局上有 match_parent 时才有效...不适用于 wrap
    • 用底部的静态按钮包装对话框的绝佳解决方案。
    【解决方案2】:

    您可以使用 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'
    • 我认为当你得到它时你应该自定义你的 ListView !你能添加你的代码和logcat吗?
    • 如果您想使用 getListView(),请将您的列表命名为 android:id="@android:id/list"
    • 很好的解决方案,但 ListView 将位于底部,而包含的项目很少。
    • 这不起作用。如果列表填满,则按钮仍保留在屏幕下方
    【解决方案3】:

    我的解决方案基于 Houcline 的 solutionListView 始终高于 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"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多