【问题标题】:RecyclerView inside a Dialog is overlapping other content of dialog对话框内的 RecyclerView 与对话框的其他内容重叠
【发布时间】:2020-01-07 06:18:03
【问题描述】:

我正在尝试制作一个看起来像这样的Dialog

这里,橙色矩形中的元素是Dialog 的固定页眉和页脚部分。蓝色矩形内的元素根据要显示的内容动态放置。 这是它的 XML。

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:id="@+id/header_section"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <!-- some views -->

        </LinearLayout>

        <LinearLayout
            android:id="@+id/body_section"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintTop_toBottomOf="@id/header_section"
            app:layout_constraintBottom_toTopOf="@id/footer_section"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent">

        </LinearLayout>

        <LinearLayout
            android:id="@+id/footer_section"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent">

            <!-- some views -->

        </LinearLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

当我将其他视图放置在蓝色区域内时它工作正常,但是当 RecyclerView 放置在其中并动态添加项目时,它会变大并与页眉部分中的文本重叠并推动页脚部分到底部。如何让RecyclerView 只覆盖蓝色区域?

【问题讨论】:

  • 为什么你需要在 ConstraintLayout 中使用线性布局?可以通过单个约束布局来实现!
  • 希望对您有所帮助:stackoverflow.com/q/33560276
  • @SantanuSur 我将不同的部分放在LinearLayout 中只是为了使代码看起来干净并保持其所有内容受到约束,而不必对页眉和页脚部分中的每个View 应用约束。
  • @ViralPatel 谢谢,但您分享的链接没有回答问题。

标签: android xml android-recyclerview


【解决方案1】:

在正文部分使用高度 0dp,使其适合页眉和页脚之间

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:id="@+id/header_section"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <!-- some views -->

        </LinearLayout>

        <LinearLayout
            android:id="@+id/body_section"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:orientation="vertical"
            app:layout_constraintTop_toBottomOf="@id/header_section"
            app:layout_constraintBottom_toTopOf="@id/footer_section"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent">

        </LinearLayout>

        <LinearLayout
            android:id="@+id/footer_section"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent">

            <!-- some views -->

        </LinearLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

【讨论】:

  • 我试过这个,但是它使正文部分消失了。
  • 你在正文部分放置了什么小部件?
  • 我在 LinearLayout 中放置了一个 RecyclerView 和 2 个 TextView
  • 添加layout_constrainedHeight="true"确实解决了RecyclerView与header内容重叠的问题。但是当添加元素时,它仍然会将页脚部分推到视野之外。
  • 给recyclerview的高度和宽度0 dp和top,bottom,start,end约束给父级,如果你的recyclerview低于textview,给textview的顶部到底部
【解决方案2】:

我使用 ScrollView 来解决这个问题,

<androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

            <ScrollView
                android:id="@+id/scrollView"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                app:layout_constraintHeight_min="100dp"
                app:layout_constraintHeight_max="250dp"
                app:layout_constrainedHeight="true"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                >

                 <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/recycler"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="16dp"
                    android:paddingHorizontal="8dp"
                    android:paddingVertical="8dp"
                   />

            </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

主要属性就是这3个

            app:layout_constraintHeight_min="100dp"
            app:layout_constraintHeight_max="250dp"
            app:layout_constrainedHeight="true"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-20
    • 1970-01-01
    • 1970-01-01
    • 2016-12-28
    • 1970-01-01
    • 2011-10-07
    相关资源
    最近更新 更多