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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2386593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:38:38  来源:igfitidea点击:

How to show a button at the end of an Android ListView

androidlistview

提问by UMAR

I want to show a button at the end of an Android list view. How can I achieve this?

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

I don't want to stick it to the activity bottom using alignparentbottom="true". Using layout_belowdoes not work for me either.

我不想使用alignparentbottom="true". 使用layout_below对我也不起作用。

My current XML:

我当前的 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>

回答by Diego Torres Milano

You may want to use ListView#addFooterView()to add a Viewat the bottom of the ListView.

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

回答by Sachin Gurnani

I do it like this fixed button at the buttom of the screen

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

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

if ur using linearLayout then assign android:layout_weight="1" to the listview and dont assign weight for button it works

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

回答by Pablo Johnson

You could do something like this:

你可以这样做:

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

回答by skyman

1 If you want to add Button as the last element of the list view

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

You must create custom ListAdapterfor your ListView which will create a view with a Button in the getView method. You should decide how to return your custom view for the last element, you can hardcode it (return element count +1 in getCount method and return custom view in getView when position > element count) or you can add element to the structure you will be taking data from (Array, Cursor etc.) and check if field of element have certain value

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

2 If you want to add element below list view

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

You should use android:layout_width attribute and make ListView and "empty" TextView (you should use it to show users that list is empty and View rendering is completed) layout_weight greater than buttons layout_weight

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

Check how it's done in Transdroids search Activity http://code.google.com/p/transdroid/source/browse/trunk/res/layout/search.xml

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

回答by SAndroidD

Use Footer view to list-view it work.

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

list_layout.xml :

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 :

页脚视图.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>

and in Activity.java

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

回答by Arunendra

Simply Provide "layout_weight = 1" of ListView.

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

Example :

例子 :

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

回答by Beto

I came here looking for an answer first but found it somewhere else...

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

It's really easy, you just need to put weight 1 to the list inside a linear layout, the other textviews/buttons/etc don't need to have any weight value.

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

Here is an example: https://bitbucket.org/generalplus/android_development/src/5a892efb0551/samples/ApiDemos/res/layout/linear_layout_9.xml

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

回答by Richard Szalay

You could, of course, use a custom adapter and specify a footer item. But you could probably also get away with putting it at the bottom of a ScrollViewand have the ListViewstretch vertically to the content:

当然,您可以使用自定义适配器并指定页脚项。但是你也可以把它放在 a 的底部ScrollViewListView垂直拉伸到内容:

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

回答by Snicolas

Well... details of implementations are not so easy if you want to create a smooth "add more" button at the end of the list. So here is some code :

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

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