LinearLayout 项目和 recyclerview 之间的稳定距离

steady distance between LinearLayout items and recyclerview

我正在寻找一种方法,使包含两个 buttonsLinearLayoutrecyclerview 相比更加稳定。特别是我试图像这样放置它,但多了一个按钮。




这是我的 xml 代码:

<?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=".ui.a.TSport">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:layout_editor_absoluteX="-26dp"
        tools:layout_editor_absoluteY="338dp">

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

        </com.google.android.material.appbar.AppBarLayout>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:padding="4dp"
            android:scrollbars="vertical"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.078" />

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

            <Button
                android:id="@+id/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:text="Button" />

            <com.google.android.material.floatingactionbutton.FloatingActionButton
                android:id="@+id/floatingActionButton"
                android:layout_width="56dp"
                android:layout_height="62dp"
                android:layout_gravity="right"
                android:layout_marginLeft="200dp"
                android:clickable="true"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.929"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="0.942"
                app:srcCompat="@drawable/ic_add_black_24dp" />


        </LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

当我有两个按钮并添加一些 items 时,问题就来了:对于每个添加的项目,包含两个按钮的线性布局向下移动直到消失。

首先,删除 LinearLaypit 并使用 ConstraintLayout。

很简单

<?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" >


    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary" />

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:padding="4dp"
        android:scrollbars="vertical"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintVertical_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/appBarLayout" />


    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:layout_margin="16dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_margin="16dp"
        android:clickable="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:srcCompat="@drawable/ic_launcher_background" />

</androidx.constraintlayout.widget.ConstraintLayout>

但只有线性布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:weightSum="10">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_weight="0.75"
        android:layout_height="0dp"
        android:layout_width="match_parent" >

        <androidx.appcompat.widget.Toolbar
            android:background="?attr/colorPrimary"
            android:id="@+id/toolbar"
            android:layout_height="?attr/actionBarSize"
            android:layout_width="match_parent" />

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:layout_weight="8.25"
        android:background="@android:color/transparent"
        android:id="@+id/recyclerView"
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:padding="4dp"
        android:scrollbars="vertical"  />

    <LinearLayout
        android:layout_weight="1"
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:weightSum="10"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button"
            android:layout_gravity="left"
            android:layout_weight="3"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:text="Button" />

        <View
            android:layout_width="0dp"
            android:layout_weight="5"
            android:layout_height="10dp"/>
        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:clickable="true"
            android:id="@+id/floatingActionButton"
            android:layout_gravity="right"
            android:layout_height="62dp"
            android:layout_width="0dp"
            android:layout_weight="2"/>


    </LinearLayout>
</LinearLayout>

如果您想将两个按钮都放在底部,则必须进行 2 处更改

首先,您需要添加 android:layout_weight="1",然后将 android:layout_height="wrap_content" 替换为 android:layout_height="0dp",以便您的 RecyclerView。这将为您的 recyclerview 提供全尺寸,直到底部视图可见。

你完整的 recyclerview 应该是这样的:

 <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@android:color/transparent"
        android:padding="4dp"
        android:scrollbars="vertical"
        />

注意:这里我从RecyclerView移除了constraint,因为它的parent是LinearLayout,所以需要在这里使用constraint。

可选: 另外我注意到您使用了 ConstraintLayout 并且它只有 1 个子 LinearLayout 并且 LinearLayout 没有给出任何约束,因此如果 ConstraintLayout 没有要求您应该删除它并且只使用 LinearLayout 作为父级。