【问题标题】:How to show a button at the end of an Android ListView如何在 Android ListView 的末尾显示一个按钮
【发布时间】:2010-03-05 12:21:53
【问题描述】:

我想在 Android 列表视图的末尾显示一个按钮。我怎样才能做到这一点?

我不想使用alignparentbottom="true" 将其粘贴到活动底部。使用layout_below 对我也不起作用。

我当前的 XML:

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/main_bg">

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <ListView
            android:id="@+id/android:list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:drawSelectorOnTop="false"
            android:cacheColorHint="#ff6a00"
            android:divider="#ff8f40"
            android:dividerHeight="1px" />

    </LinearLayout>

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
            android:layout_height="50sp"
        android:background="#676767"
        android:orientation="vertical">

        <Button
            android:layout_width="100px"
            android:layout_height="wrap_content"
            android:id="@+id/btnGetMoreResults"
            android:layout_marginLeft="10px"
            android:text="Get more" />

    </RelativeLayout>

</RelativeLayout>

【问题讨论】:

标签: android listview


【解决方案1】:

您可能想要使用ListView#addFooterView()ListView 的底部添加View

【讨论】:

  • 是的,谢谢。最后创建了动态按钮,然后使用 getListView().addFooterView (DynamicButton);现在我可以在列表视图的末尾看到按钮了。
【解决方案2】:

我喜欢屏幕底部的这个固定按钮

   <RelativeLayout
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/listView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/btn_New" >
    </ListView>

    <Button
        android:id="@+id/btn_New"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginBottom="20dp"
        android:text="@string/New"
        android:width="170dp"
        android:layout_alignParentBottom="true" />
</RelativeLayout>

如果你使用 linearLayout 然后将 android:layout_weight="1" 分配给列表视图并且不为按钮分配权重它可以工作

【讨论】:

  • 我不明白为什么这个答案被忽略了。它对我很有用,谢谢!我刚刚在我的编辑中对 ListView 的高度做了一个小调整,这有助于显示只有几个项目的列表。
  • 在所有解决方案中,我都在寻找我所面临的类似问题......这个解决方案效果最好......非常感谢你!
  • 这个答案很有帮助!并且工作完美
  • 在我的情况下,我的按钮变形以占据屏幕的其余部分
  • ScrollView 围绕 ListView (也滚动)不是一个好主意。你可能会发现它开始表现得很奇怪。
【解决方案3】:

你可以这样做:

final Button btnAddMore = new Button(this);
btnAddMore.setText(R.string.art_btn_moreIssues);
exArticlesList = (ExpandableListView) this.findViewById(R.id.art_list_exlist);
exArticlesList.addFooterView(btnAddMore);

【讨论】:

    【解决方案4】:

    1如果要添加Button作为列表视图的最后一个元素

    您必须为您的 ListView 创建自定义 ListAdapter,这将在 getView 方法中创建一个带有 Button 的视图。您应该决定如何为最后一个元素返回自定义视图,您可以对其进行硬编码(在 getCount 方法中返回元素计数 +1,并在位置 > 元素计数时在 getView 中返回自定义视图),或者您可以将元素添加到您将成为的结构中从(数组、光标等)获取数据并检查元素的字段是否具有特定值

    2 如果要在列表视图下方添加元素

    你应该使用 android:layout_width 属性并使 ListView 和“空”TextView(你应该使用它来向用户显示列表是空的并且视图渲染完成) layout_weight 大于按钮 layout_weight

    在 Transdroids 搜索活动 http://code.google.com/p/transdroid/source/browse/trunk/res/layout/search.xml 中检查它是如何完成的

    【讨论】:

    • 我不是在谈论将按钮放在列表视图中,我只想在列表视图的底部放置一个按钮。我认为你没有正确理解我的问题。
    • 要将任何数据添加到 ListView,您必须创建要添加的 View。控制 ListView 中显示的对象是它的适配器。标准适配器(SimpleAdapter、SimpleCursorAdapter、ArrayAdapter)只能从单一布局创建项目,因此您必须创建自己的适配器(不能像摇摆 JTable 那样只将项目添加到 ListView)。
    • 感谢您的回复。但我对您在列表视图中使用单词添加感到困惑。在我的情况下,我不想将按钮添加到列表视图,而是在列表视图数据结束的地方显示。那为什么它与列表视图有关系以使其自定义?
    • 好吧,也许我真的不明白。您希望按钮位于 ListView“中”(作为最后一个元素)还是位于 ListView 下方和屏幕底部?
    • 编辑答案以同时拥有两种解决方案。
    【解决方案5】:

    使用页脚视图来列表查看它的工作原理。

    list_layout.xml:

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    
        <ListView
            android:id="@+id/list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:drawSelectorOnTop="false"
            android:cacheColorHint="#ff6a00"
            android:divider="#ff8f40"
            android:dividerHeight="1px" />
    
    </LinearLayout>
    

    footerview.xml:

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
        <Button
            android:layout_width="100px"
            android:layout_height="wrap_content"
            android:id="@+id/btnGetMoreResults"
            android:layout_marginLeft="10px"
            android:text="Get more" />
    </FrameLayout>
    

    在 Activity.java 中

        list=(ListView)findViewById(R.id.list);
    
        FrameLayout footerLayout = (FrameLayout) getLayoutInflater().inflate(R.layout.footerview,null);
        btnPostYourEnquiry = (Button) footerLayout.findViewById(R.id.btnGetMoreResults);
    
        list.addFooterView(footerLayout);
    

    【讨论】:

      【解决方案6】:

      只需提供 ListView 的“layout_weight = 1”即可。

      例子:

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
          <ListView
              android:id="@+id/android:list"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:drawSelectorOnTop="false"
              android:cacheColorHint="#ff6a00"
              android:divider="#ff8f40"
              android:layout_weight="1"
              android:dividerHeight="1px" />
          <Button
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_gravity="bottom"
              android:layout_marginLeft="10dp"
              android:layout_marginRight="10dp"
              android:background="#ff8f40"
              android:padding="20dp"
              android:text="Click"
              android:textColor="#FFFFFF"
              android:textSize="22sp"
              />
      </LinearLayout>
      

      【讨论】:

      • 这样按钮总是显示在它的下方,这可能不是我们想要的解决方案。
      【解决方案7】:

      我先来这里寻找答案,但在其他地方找到了它......

      真的很简单,你只需要将权重1放在线性布局内的列表中,其他文本视图/按钮/等不需要任何权重值。

      这里是一个例子:https://bitbucket.org/generalplus/android_development/src/5a892efb0551/samples/ApiDemos/res/layout/linear_layout_9.xml

      【讨论】:

        【解决方案8】:

        当然,您可以使用自定义适配器并指定页脚项。但是您也可以将它放在ScrollView 的底部并让ListView 垂直延伸到内容:

        <?xml version="1.0" encoding="UTF-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@drawable/main_bg">
        
          <ScrollView
              android:layout_height="fill_parent"
              android:layout_width="fill_parent">
        
            <LinearLayout 
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                    >
        
              <ListView android:id="@+id/android:list"
                  android:layout_width="fill_parent" 
                  android:layout_height="wrap_content"
                  android:drawSelectorOnTop="false" 
                  android:cacheColorHint="#ff6a00"
                  android:divider="#ff8f40"
                  android:dividerHeight="1px"
                  />
        
              <RelativeLayout 
                  android:layout_width="fill_parent"
                  android:layout_height="50sp"
                  android:background="#676767"
                  android:orientation="vertical">
        
                <Button android:layout_width="100px"
                    android:layout_height="wrap_content"
                    android:id="@+id/btnGetMoreResults"
                    android:layout_marginLeft="10px"
                    android:text="Get more" />
        
              </RelativeLayout>
        
            </LinearLayout>
        
          </ScrollView>
        
        </RelativeLayout>
        

        【讨论】:

        • 在执行此按钮时仍然没有希望不显示布局我增加了布局高度但仍然不可见。
        • 嗨...我也遇到了同样的问题,让我帮忙解决这个问题。
        【解决方案9】:

        嗯...如果您想在列表末尾创建一个平滑的“添加更多”按钮,那么实现的细节就不是那么容易了。所以这里有一些代码:

        myArrayAdapter = new ArrayAdapter<Show>(this, android.R.layout.simple_list_item_1, myList) {
        
             @Override
             public View getView(int position, View convertView, ViewGroup parent) {
        
                if( position >= super.getCount()  )
                    return buttonMore ;
        
                MyNormalView view = null; 
                if (convertView == null || convertView instanceof Button )
                    view = new MyNormalView(getContext());
                else
                    view = (MyNormalView) convertView;
        
                //customize view here with data
                return view;
            }
        
            @Override
            public int getCount() {
               return super.getCount()+1;
            }//met
        };
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-03-27
          • 2014-04-10
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多