【问题标题】:Android: Show button just below to the recylerview but always on the screenAndroid:在recyclerview下方显示按钮,但始终在屏幕上
【发布时间】:2021-11-21 11:36:30
【问题描述】:

在屏幕上,我想显示从顶部开始的 RecyclerView,在 RecyclerView 的末尾,有一个按钮。

  1. 当 RecyclerView 没有占据整个屏幕时,Button 将位于 RecyclerView 的正下方。
  2. 当 RecylerView 可滚动时,在这种情况下,按钮将位于屏幕底部。

我尝试了this 代码。但这里的问题是,RecyclerView 处于中心位置。就我而言,它总是从顶部开始(在操作栏之后)。

我尝试了以下代码:

主布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:background="#b6d7a8" >

<LinearLayout
    android:id="@+id/actionBarTermsAndCondition"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/dark_gray"
    android:gravity="center_vertical"
    android:minHeight="70dp"
    android:orientation="horizontal"
    android:paddingStart="16dp"
    android:paddingTop="10dp"
    android:paddingEnd="16dp"
    android:paddingBottom="10dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" >

    <ImageView
        android:id="@+id/closeButton"
        android:layout_width="@dimen/spacing_2x"
        android:layout_height="@dimen/spacing_2x"
        android:layout_gravity="center_vertical"
        android:layout_marginEnd="@dimen/spacing_2_5x"
        android:clickable="true"
        android:contentDescription="@string/close"
        android:foreground="?selectableItemBackground"
        android:src="@drawable/cross"
        android:visibility="invisible" />

    <TextView
        android:id="@+id/route"
        style="@style/ScreenTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:contentDescription="Import Venue"
        android:fontFamily="@font/roboto"
        android:text="Import Venue"
        android:textColor="@color/white"
        tools:text="Something" />
</LinearLayout>

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/list"
    android:name="DataManagementFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layoutManager="LinearLayoutManager"
    app:layout_constrainedHeight="true"
    app:layout_constraintBottom_toTopOf="@id/constraint_layout_cloud_access"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/actionBarTermsAndCondition"
    app:layout_constraintVertical_chainStyle="packed"
    tools:itemCount="3"
    tools:listitem="@layout/fragment_data_management_list_row" />

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/constraint_layout_cloud_access"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FEAB8C"
    android:paddingStart="16dp"
    android:paddingTop="10dp"
    android:paddingBottom="21dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/list">

    <TextView
        android:id="@+id/text_cloud_access"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/roboto"
        android:text="Cloud access"
        android:textColor="#3C3C41"
        android:textSize="16dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="Cloud access" />

    <TextView
        android:id="@+id/text_import_venue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/roboto"
        android:text="Import venue"
        android:textColor="#3C3C41"
        android:textSize="16dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/text_cloud_access"
        tools:text="Import place" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

RecyclerView 行布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:background="#fff2cc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingEnd="@dimen/spacing_3x"
android:paddingStart="@dimen/spacing_2x"
android:paddingBottom="@dimen/spacing_2_5x"
android:paddingTop="@dimen/spacing_2_5x"
android:orientation="horizontal"
tools:showIn="@layout/fragment_data_management">

<TextView
    android:id="@+id/text_floor_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="@font/roboto"
    android:textAppearance="?attr/textAppearanceListItem"
    android:textColor="#3C3C41"
    android:textSize="16dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:text="Preloaded" />

<TextView
    android:id="@+id/text_venue_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="@font/roboto"
    android:textAppearance="?attr/textAppearanceListItem"
    android:textColor="#636367"
    android:textSize="12dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/text_floor_name"
    tools:text="ABC" />

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="?android:attr/listChoiceIndicatorMultiple"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:srcCompat="?attr/dividerHorizontal" />

</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

    标签: android android-layout android-recyclerview android-constraintlayout


    【解决方案1】:

    您的 RecyclerView 在其他两个布局之间居中。添加

    app:layout_constraintVertical_bias="0.0"
    

    RecyclerView 的 XML 以将其移动到顶部。您可以在 ConstraintLayout 文档中查看偏差的工作原理。

    【讨论】:

      【解决方案2】:

      我认为你可以通过指导实现你想要的。步骤如下:

      1. 创建一个位于屏幕 90% 的水平参考线,这意味着它将非常靠近底部,左侧 10% 是当回收站视图变为可滚动时您的按钮所在的位置。
      2. 设置回收站视图的底部约束以匹配指南的顶部,并且回收站视图的顶部与屏幕顶部相匹配。这将导致回收站视图出现在屏幕的一半上。
      3. 将回收站视图的垂直偏差设置为 0.0,这会将回收站视图固定在顶部。
      4. 设置按钮的顶部约束以匹配回收器视图的底部,这将导致它会粘在回收器视图的底部,但是由于回收器视图(感谢指南)只会增长到 90 % 的屏幕,按钮将根据需要位于屏幕底部。

      将以下属性添加到回收站视图中很重要:

      app:layout_constrainedHeight="true"
      

      与按钮和回收器视图相关的布局应如下所示:

      <?xml version="1.0" encoding="utf-8"?>
      <androidx.constraintlayout.widget.ConstraintLayout 
      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=".MainActivity">
      
      <androidx.recyclerview.widget.RecyclerView
          android:id="@+id/list"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          app:layout_constrainedHeight="true"
          app:layout_constraintTop_toTopOf="parent"
          app:layout_constraintBottom_toTopOf="@+id/guideline"
          app:layout_constraintVertical_bias="0.0"
          tools:itemCount="130"/>
      
      <Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Button text"
          app:layout_constraintTop_toBottomOf="@+id/list"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintStart_toStartOf="parent"/>
      
      <androidx.constraintlayout.widget.Guideline
          android:id="@+id/guideline"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal"
          app:layout_constraintGuide_percent="0.9"/>
      
      </androidx.constraintlayout.widget.ConstraintLayout>
      

      项目很少:

      有很多项目:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-19
        • 2015-06-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多