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

UMA*_*MAR 41 android listview

我想在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>
Run Code Online (Sandbox Code Playgroud)

Die*_*ano 74

您可能希望使用ListView#addFooterView()View在底部添加a ListView.


Sac*_*ani 34

我在屏幕的按钮处就像这个固定按钮一样

   <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>
Run Code Online (Sandbox Code Playgroud)

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

  • 我不明白为什么这个答案被忽略了.这对我很有用,谢谢!我只是在编辑中对ListView高度进行了一些小调整,这有助于显示只有少数项目的列表. (2认同)

Pab*_*son 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);
Run Code Online (Sandbox Code Playgroud)


sky*_*man 7

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

您必须为ListView 创建自定义ListAdapter,它将在getView方法中使用Button创建视图.你应该决定怎样来回报您的自定义视图的最后一个元素,你可以硬编码(归元计数getCount将法+1和getView当位置>元素计数返回自定义视图),或者你可以到你将是结构中添加元素从(Array,Cursor等)获取数据并检查元素的字段是否具有特定值

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

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

检查在Transdroids搜索活动中的工作方式http://code.google.com/p/transdroid/source/browse/trunk/res/layout/search.xml


SAn*_*idD 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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

并在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);
Run Code Online (Sandbox Code Playgroud)