【问题标题】:(android) How to place a view below other views in constraint layout(android) 如何在约束布局中将视图放置在其他视图下方
【发布时间】:2021-01-23 03:53:30
【问题描述】:

这张照片是layout的状态

我想将RecyclerView 放在Button 下方

但是,如您所见,即使使用layout_constraintTop_toBottomOfRecyclerView 也没有放置

Button

有一个条件,RecyclerView的大小必须是math_parent

这是因为您使用的是嵌套的recycler view,并且您正在动态添加项目,否则

动态添加时最后嵌套的recycler view's item会被截断,不可见..

如果我在约束布局中将视图的高度设置为 match_parent,有什么方法可以放置它

低于其他视图?

这是 XML 文件。

activity_write_routine.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".data.DailyRecordDetailActivity">
    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/Theme.AppBarOverlay"
        app:elevation="0dp">
        <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <TextView
                    android:id="@+id/body_part_detail_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="P A R T"
                    android:textColor="@color/white"
                    android:textAppearance="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
                    android:layout_centerHorizontal="true"/>
            </RelativeLayout>
        </androidx.appcompat.widget.Toolbar>
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="10dp"
        app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">
        <Button
            android:id="@+id/add_routine"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="A D D"
            android:backgroundTint="@color/light_green"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintRight_toRightOf="parent"/>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/routine_recyclerview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical"
            app:layout_constraintTop_toBottomOf="@+id/add_routine"/>
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

【问题讨论】:

    标签: android android-recyclerview android-constraintlayout


    【解决方案1】:

    将高度改为0dp,并将recyclerview的底部约束为父级

    <androidx.recyclerview.widget.RecyclerView
          android:id="@+id/routine_recyclerview"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:scrollbars="vertical"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintTop_toBottomOf="@+id/add_routine"/>
    

    【讨论】:

    • 抱歉,这不是wrap_contetnt 之类的吗? RecyclerView 的高度是match_parent,我想把它放在按钮下面。
    • 抱歉,我不小心忘记了密码。你的代码是正确的答案。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-20
    • 2021-11-24
    • 2020-03-22
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 2018-04-18
    相关资源
    最近更新 更多