具有固定页眉,可滚动主体和固定页脚的DialogFragment

时间:2019-08-05 08:09:12

标签: android scrollview android-constraintlayout android-dialogfragment

我想显示一个带有标题(页眉),正文(内容)和接受按钮(页脚)的对话框。我希望即使内容长于屏幕高度,页眉和页脚也可见。如果是这种情况,我希望身体可以滚动以便可以收缩。这是我尝试过的,scrollView获取所有对话框空间,不显示页眉和页脚:

<?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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true"
    android:minWidth="300dp">

    <TextView
        android:id="@+id/title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@android:color/darker_gray"
        android:text="Title"
        app:layout_constraintBottom_toTopOf="@+id/bodyContainer"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ScrollView
        android:id="@+id/bodyContainer"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/buttonContainer"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/title">

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

            <TextView
                android:id="@+id/text"
                android:layout_width="match_parent"
                android:layout_height="1500dp"
                android:text="Long text" />
        </LinearLayout>
    </ScrollView>

    <LinearLayout
        android:id="@+id/buttonContainer"
        android:layout_width="0dp"
        android:layout_height="30dp"
        android:background="@android:color/darker_gray"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/bodyContainer" />

</androidx.constraintlayout.widget.ConstraintLayout>

2 个答案:

答案 0 :(得分:0)

ScrollView's的高度设置为wrap_content时,如果约束尺寸太大而无法满足要求,则约束不会限制尺寸。要解决此问题并施加最高和最低约束,请为app:layout_constrainedHeight="true"设置ScrollView

答案 1 :(得分:0)

这正是您想要的。使用NestedScrollView而不是ScrollView:

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minWidth="300dp">
<TextView
    android:id="@+id/title"
    android:layout_width="411dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    android:background="@android:color/darker_gray"
    android:text="Title"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0" />
<androidx.core.widget.NestedScrollView
    android:id="@+id/bodyContainer"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/title"
    app:layout_constraintBottom_toTopOf="@+id/button">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:id="@+id/text"
            android:layout_width="match_parent"
            android:layout_height="1500dp"
            android:text="Long text" />
    </LinearLayout>
</androidx.core.widget.NestedScrollView>
<Button
    android:id="@+id/button"
    android:layout_width="411dp"
    android:layout_height="30dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    android:background="@android:color/darker_gray"
    android:orientation="vertical"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="1.0" />
</androidx.constraintlayout.widget.ConstraintLayout>