在 ListView 末尾添加一个 Button

Adding a Button at the end of ListView

我想在列表视图的末尾添加一个按钮。我有以下 xml 文件。列表视图及其内容出现,但按钮不显示。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:minWidth="25px"
    android:minHeight="25px">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/myListView" />
    <Button
        android:text="Project Download"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/download" />
</LinearLayout>

问题出在您的 ListView 中的属性 android:layout_height="match_parent"match_parent表示listview会填满所有space。正确的方法是:

<?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"
    android:minWidth="25px"
    android:minHeight="25px">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:weight="1"
        android:id="@+id/myListView" />
    <Button
        android:text="Project Download"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/download" />
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">

<Button
    android:layout_alignParentBottom="true"
    android:text="Project Download"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/download" />

<ListView
    android:layout_above="@id/download"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/myListView" /></RelativeLayout>

您的按钮未显示,因为您的 ListView 在将其 layout_height 设置为 [ 时将整个 space 隐藏了您的按钮=31=]match_parent 将占用整个可用 space。所以把它改成 wrap_content 像这样:

<?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"
    android:minWidth="25px"
    android:minHeight="25px">
    <ListView
        android:layout_width="fill_parent"
        android:layout_height="wrap_parent"
        android:id="@+id/myListView" />
    <Button
        android:text="Project Download"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/download" />
</LinearLayout>

wrap_content 会将您的视图包装到最不可用的 height/width 而不会隐藏任何内容或使任何内容不清楚.

我也遇到了同样的问题,已经解决了。 您只需要将该布局包装到 ScrollView

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/rv_transaction_history"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1">

            </android.support.v7.widget.RecyclerView>

            <Button
                android:id="@+id/btn_history_load_more"
                style="?borderlessButtonStyle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="sans-serif-medium"
                android:paddingBottom="@dimen/margin_big"
                android:paddingTop="@dimen/margin_large"
                android:text="@string/text_btn_load_more"
                android:textColor="#009CDE"
                android:textSize="@dimen/text_size_medium" />
    </LinearLayout>
</ScrollView>