如何在Android ListView的末尾显示一个按钮

41

我想在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>

3
像这样:http://blog.maxaller.name/2010/05/attaching-a-sticky-headerfooter-to-an-android-listview/? - Thomas Ahle
请查看此链接:https://dev59.com/L3E95IYBdhLWcg3wQ7uc#22319421 - Zahid Habib
9个回答

74

是的,谢谢你。 最终创建了动态按钮,然后使用 getListView().addFooterView ( DynamicButton);现在我可以在列表视图的末尾看到按钮。 - UMAR-MOBITSOLUTIONS

35
我做的是,在屏幕底部有一个固定按钮。
   <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,而不要为按钮分配权重,这样就可以正常工作了。


1
在我的情况下,我的按钮会变形以占据屏幕的其余部分。 - Thomas
ScrollView 包围 ListView(也会滚动)不是一个好主意。你可能会发现它开始表现得很奇怪。 - Ghoti
@Thomas,我的按钮浮在了<ListView>上方。如果你也遇到了这个问题,请尝试从你的<Button>中移除这个属性:android:layout_centerVertical="true" - Junior Mayhé

15

你可以像这样进行操作:

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

7

1 如果你想将按钮添加为列表视图的最后一个元素

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

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

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

查看Transdroids搜索Activity中的实现方式http://code.google.com/p/transdroid/source/browse/trunk/res/layout/search.xml


我不是在说把按钮放在列表视图中,我只想把一个单独的按钮放在列表视图底部。我觉得你没有完全理解我的问题。 - UMAR-MOBITSOLUTIONS
要将任何数据添加到ListView中,必须创建将被添加的View。控制在ListView中显示内容的对象是它的Adapter。标准适配器(SimpleAdapter、SimpleCursorAdapter、ArrayAdapter)只能从单个布局创建项,因此您必须创建自己的适配器(您不能像swing JTable一样仅向ListView添加项)。 - skyman
谢谢您的回复。但是我对您在列表视图中使用“添加”一词感到困惑。 在我的情况下,我不想将按钮添加到列表视图中,而是在列表视图数据结束后显示。那么为什么它与列表视图有关系,需要进行自定义呢? - UMAR-MOBITSOLUTIONS
好的,也许我真的没有理解清楚。 您想要按钮在ListView中(作为最后一个元素)还是在ListView下方并位于屏幕底部? - skyman
修改答案以包含两个解决方案。 - skyman

6

请使用页脚视图来处理列表视图。

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

4
只需在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>

3
这样按钮就始终显示在它下面,这可能不是期望的解决方案。 - vantesllar

3

0

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

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

0
当然,您可以使用自定义适配器并指定页脚项。但是您也可以将其放在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>

尽管我增加了布局高度,但仍然看不到此按钮,我感到非常无助。 - UMAR-MOBITSOLUTIONS
嗨...我也遇到了同样的问题,让我来帮你解决这个问题。 - MGSenthil

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接