【问题标题】:How to Place a Button below a ListView in a Scrollable Activity?如何在可滚动活动中的 ListView 下方放置一个按钮?
【发布时间】:2020-01-25 20:38:04
【问题描述】:
<?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或者LinearLayout都可以

标签: android listview scroll xml-layout


【解决方案1】:

对于你想要的行为,如果你使用 RecyclerView 而不是 ListView 并让它在 NestedScrollView 内,然后在 RecyclerView 上启用 nestedScrolling 属性会更好。这样,您的 RecyclerView 将表现得像一个长列表,并且它下面的所有视图只有在列表用完后才可见。 查看answers in this link 了解其他人如何实现它。

【讨论】:

    【解决方案2】:

    因此,为此您必须将列表和按钮放在 NestedScrollView 中,但 Nestedscrollview 仅支持单个子项,因此首先您必须将 Listbutton 放入布局中,然后放入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>
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        将listView的高度设为固定,然后将按钮放在其下方。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-12-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-03-12
          • 2012-02-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多