簡體   English   中英

如何在可滾動活動中的 ListView 下方放置一個按鈕?

[英]How to Place a Button below a ListView in a Scrollable Activity?

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".list1">

            <ImageView
                android:id="@+id/sinb"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/left"
                android:padding="10dp"
                android:layout_marginTop="20dp"
                android:layout_marginLeft="20dp"
                />


            <ListView
                android:layout_below="@id/sinb"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/listView1"
                android:layout_marginTop="30dp"
                android:layout_centerHorizontal="true"
                android:choiceMode="multipleChoice" />
            <Button
                android:id="@+id/btn1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/listView1"
                android:text="submit"/>

        </RelativeLayout>

我已將這些放在相對布局中。我嘗試使用 ScrollView,但它不起作用。 我想要的是一個提交按鈕應該顯示在 Listview 下方,當列表增長並離開屏幕時,活動變得可滾動並且按鈕應該顯示在 listview 的末尾下方。

對於您想要的行為,如果您使用 RecyclerView 而不是 ListView 並讓它位於 NestedScrollView 內,然后在 RecyclerView 上啟用 nestedScrolling 屬性,那會更好。 這樣,您的 RecyclerView 將表現得像一個長列表,並且它下面的所有視圖只有在列表用完后才可見。 檢查此鏈接中的答案以了解其他人如何實現它。

因此,為此您必須將 List 和按鈕放在NestedScrollView中,但Nestedscrollview僅支持單個子項,因此首先您必須將Listbutton放在 Layout 中,然后將布局放在NestedScrollView中,您可以參考以下內容代碼:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools">

    <ImageView
        android:id="@+id/sinb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:padding="10dp"
        android:src="@drawable/left" />

    <android.support.v4.widget.NestedScrollView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_below="@id/sinb">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ListView
                android:id="@+id/listView1"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginTop="30dp"
                tools:listitem="@layout/item_view_note"
                android:choiceMode="multipleChoice" />

            <Button
                android:id="@+id/btn1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/listView1"
                android:text="submit" />
        </RelativeLayout>

    </android.support.v4.widget.NestedScrollView>
</RelativeLayout>

添加 ScrollView/NestedScrollView 作為父布局並在滾動視圖中添加視圖(ListView 和 Button )

試試這個代碼:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".list1">

                <ImageView
                    android:id="@+id/sinb"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/left"
                    android:padding="10dp"
                    android:layout_marginTop="20dp"
                    android:layout_marginLeft="20dp"
                    />
        <android.support.v4.widget.NestedScrollView
                            android:id="@+id/navigation_nested"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:fillViewport="true"
                              android:layout_below="@+id/sinb"
                            android:overScrollMode="never">
         <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    android:padding="@dimen/margin_15">
                         <ListView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/listView1"
                    android:layout_marginTop="30dp"
                    android:layout_centerHorizontal="true"
                    android:choiceMode="multipleChoice" />
                <Button
                    android:id="@+id/btn1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="submit"/>
</LinearLayout>
    </android.support.v4.widget.NestedScrollView>
    </RelativeLayout >

將 listView 的高度設為固定,然后將按鈕放在其下方。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM