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

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

我不想使用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> 

您可能希望使用ListView#addFooterView()在ListView的底部添加一个View

我这样做就像这个固定的button在屏幕的button

  <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”到列表视图和不分配重量的button,它的工作原理

你可以做这样的事情:

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

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

您必须为您的ListView创build自定义的ListAdapter ,它将在getView方法中使用Button创build一个视图。 您应该决定如何返回最后一个元素的自定义视图,您可以对其进行硬编码(在getCount方法中返回元素数+1,并在position>元素数时返回getView中的自定义视图),也可以将元素添加到结构中从(Array,Cursor等)中取数据并检查元素的字段是否有一定的值

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

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

检查如何在Transdroidssearch活动http://code.google.com/p/transdroid/source/browse/trunk/res/layout/search.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:

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

简单提供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> 

我来到这里寻找答案,但发现在其他地方…

这很容易,你只需要在线性布局内放置重量1,其他textviews /button/等不需要有任何权重值。

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

当然,您可以使用自定义适配器并指定页脚项目。 但是你也可能会把它放在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> 

那么…如果你想在列表的最后创build一个平滑的“添加更多”button,实现的细节不是那么容易。 所以这里是一些代码:

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