android:DialogFragment 的布局不显示 NestedScrollView

如何解决android:DialogFragment 的布局不显示 NestedScrollView

我为 DialogFragment 创建了一个布局,但它没有正确显示。在 Android Studio 中,布局的预览显示正确(见下图)。但是当我运行应用程序时,所有插入的 nestedScrollView 都没有显示。我将错误归结为 nestedScrollView 的这一行:android:layout_height="0dp"。当我将 0dp 更改为 wrap content 时,所有元素都会显示,但现在它们与其他元素重叠。

有人可以给我建议如何解决这个问题吗?

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="8dp"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:id="@+id/fr_tv_lbl_list_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/listName"
        android:textSize="@dimen/textSize_22"
        android:textStyle="bold"
        android:padding="8dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />
    <EditText
        android:id="@+id/fr_et_list_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/listName"
        android:textSize="@dimen/textSize_18"
        android:layout_marginStart="8dp"
        android:inputType="textAutoCorrect"
        android:importantForAutofill="no"
        app:layout_constraintTop_toBottomOf="@id/fr_tv_lbl_list_name"
        app:layout_constraintStart_toStartOf="@id/fr_tv_lbl_list_name"
        />

    <View
        android:id="@+id/fr_divider"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="@color/purple_700"
        android:layout_marginTop="12dp"
        app:layout_constraintTop_toBottomOf="@id/fr_et_list_name"
        app:layout_constraintStart_toStartOf="@id/fr_tv_lbl_list_name"
        />

    <TextView
        android:id="@+id/fr_tv_lbl_list_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/listItems"
        android:textStyle="bold"
        android:textSize="@dimen/textSize_22"
        android:layout_marginTop="12dp"
        android:padding="8dp"
        app:layout_constraintTop_toBottomOf="@id/fr_divider"
        app:layout_constraintStart_toStartOf="@id/fr_tv_lbl_list_name"
        />
    <ImageView
        android:id="@+id/fr_img_add_entry"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_marginStart="24dp"
        android:src="@drawable/ic_add_circle_24"
        android:contentDescription="@string/contentDescriptionAddListItem"
        app:layout_constraintStart_toEndOf="@id/fr_tv_lbl_list_item"
        app:layout_constraintTop_toTopOf="@id/fr_tv_lbl_list_item"
        app:layout_constraintBottom_toBottomOf="@id/fr_tv_lbl_list_item"
        />
    <TextView
        android:id="@+id/fr_tv_error"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/fr_max_items"
        android:textSize="@dimen/textSize_18"
        android:gravity="center"
        android:textColor="?attr/colorError"
        android:layout_marginStart="16dp"
        app:layout_constraintBottom_toBottomOf="@id/fr_tv_lbl_list_item"
        app:layout_constraintStart_toEndOf="@id/fr_img_add_entry"
        app:layout_constraintEnd_toEndOf="parent"
        android:visibility="invisible"/>


    <androidx.core.widget.nestedScrollView
        android:id="@+id/fr_scrollview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="12dp"
        android:scrollbars="vertical"
        app:layout_constraintTop_toBottomOf="@id/fr_tv_lbl_list_item"
        app:layout_constraintBottom_toTopOf="@id/fr_layout_btn"
        app:layout_constraintStart_toStartOf="@id/fr_et_list_name">

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

            <EditText
                android:id="@+id/fr_et_list_entry1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:hint="@string/listItemHint"
                android:textSize="@dimen/textSize_18"
                android:layout_marginStart="8dp"
                android:layout_marginEnd="8dp"
                android:inputType="textAutoCorrect"
                android:importantForAutofill="no"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toStartOf="@id/fr_img_delete_entry1"
                />
            <ImageView
                android:id="@+id/fr_img_delete_entry1"
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:src="@drawable/ic_remove_circle_24"
                android:layout_marginEnd="8dp"
                android:contentDescription="@string/contentDescriptionDeleteListItem"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="@id/fr_et_list_entry1"
                app:layout_constraintBottom_toBottomOf="@id/fr_et_list_entry1"
                />

            <EditText
                android:id="@+id/fr_et_list_entry2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:hint="@string/listItemHint"
                android:textSize="@dimen/textSize_18"
                android:layout_marginStart="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginTop="8dp"
                android:inputType="textAutoCorrect"
                android:importantForAutofill="no"
                app:layout_constraintTop_toBottomOf="@id/fr_img_delete_entry1"
                app:layout_constraintStart_toStartOf="@id/fr_et_list_entry1"
                app:layout_constraintEnd_toStartOf="@id/fr_img_delete_entry2"
                android:visibility="visible"
                />
            <ImageView
                android:id="@+id/fr_img_delete_entry2"
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:src="@drawable/ic_remove_circle_24"
                android:layout_marginEnd="8dp"
                android:contentDescription="@string/contentDescriptionDeleteListItem"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="@id/fr_et_list_entry2"
                app:layout_constraintBottom_toBottomOf="@id/fr_et_list_entry2"
                android:visibility="visible"
                />

            <EditText
                android:id="@+id/fr_et_list_entry3"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:hint="@string/listItemHint"
                android:textSize="@dimen/textSize_18"
                android:layout_marginStart="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginTop="8dp"
                android:inputType="textAutoCorrect"
                android:importantForAutofill="no"
                app:layout_constraintTop_toBottomOf="@id/fr_img_delete_entry2"
                app:layout_constraintStart_toStartOf="@id/fr_et_list_entry1"
                app:layout_constraintEnd_toStartOf="@id/fr_img_delete_entry1"
                android:visibility="visible"
                />
            <ImageView
                android:id="@+id/fr_img_delete_entry3"
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:src="@drawable/ic_remove_circle_24"
                android:layout_marginEnd="8dp"
                android:contentDescription="@string/contentDescriptionDeleteListItem"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="@id/fr_et_list_entry3"
                app:layout_constraintBottom_toBottomOf="@id/fr_et_list_entry3"
                android:visibility="visible"
                />

            <EditText
                android:id="@+id/fr_et_list_entry4"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:hint="@string/listItemHint"
                android:textSize="@dimen/textSize_18"
                android:layout_marginStart="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginTop="8dp"
                android:inputType="textAutoCorrect"
                android:importantForAutofill="no"
                app:layout_constraintTop_toBottomOf="@id/fr_img_delete_entry3"
                app:layout_constraintStart_toStartOf="@id/fr_et_list_entry1"
                app:layout_constraintEnd_toStartOf="@id/fr_img_delete_entry1"
                android:visibility="visible"
                />
            <ImageView
                android:id="@+id/fr_img_delete_entry4"
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:src="@drawable/ic_remove_circle_24"
                android:layout_marginEnd="8dp"
                android:contentDescription="@string/contentDescriptionDeleteListItem"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="@id/fr_et_list_entry4"
                app:layout_constraintBottom_toBottomOf="@id/fr_et_list_entry4"
                android:visibility="visible"
                />

            <EditText
                android:id="@+id/fr_et_list_entry5"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:hint="@string/listItemHint"
                android:textSize="@dimen/textSize_18"
                android:layout_marginStart="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginTop="8dp"
                android:inputType="textAutoCorrect"
                android:importantForAutofill="no"
                app:layout_constraintTop_toBottomOf="@id/fr_img_delete_entry4"
                app:layout_constraintStart_toStartOf="@id/fr_et_list_entry1"
                app:layout_constraintEnd_toStartOf="@id/fr_img_delete_entry1"
                android:visibility="visible"
                />
            <ImageView
                android:id="@+id/fr_img_delete_entry5"
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:src="@drawable/ic_remove_circle_24"
                android:layout_marginEnd="8dp"
                android:contentDescription="@string/contentDescriptionDeleteListItem"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="@id/fr_et_list_entry5"
                app:layout_constraintBottom_toBottomOf="@id/fr_et_list_entry5"
                android:visibility="visible"
                />

            <EditText
                android:id="@+id/fr_et_list_entry6"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:hint="@string/listItemHint"
                android:textSize="@dimen/textSize_18"
                android:layout_marginStart="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginTop="8dp"
                android:inputType="textAutoCorrect"
                android:importantForAutofill="no"
                app:layout_constraintTop_toBottomOf="@id/fr_img_delete_entry5"
                app:layout_constraintStart_toStartOf="@id/fr_et_list_entry1"
                app:layout_constraintEnd_toStartOf="@id/fr_img_delete_entry1"
                android:visibility="visible"
                />
            <ImageView
                android:id="@+id/fr_img_delete_entry6"
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:src="@drawable/ic_remove_circle_24"
                android:layout_marginEnd="8dp"
                android:contentDescription="@string/contentDescriptionDeleteListItem"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="@id/fr_et_list_entry6"
                app:layout_constraintBottom_toBottomOf="@id/fr_et_list_entry6"
                android:visibility="visible"
                />

            <EditText
                android:id="@+id/fr_et_list_entry7"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:hint="@string/listItemHint"
                android:textSize="@dimen/textSize_18"
                android:layout_marginStart="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginTop="8dp"
                android:inputType="textAutoCorrect"
                android:importantForAutofill="no"
                app:layout_constraintTop_toBottomOf="@id/fr_img_delete_entry6"
                app:layout_constraintStart_toStartOf="@id/fr_et_list_entry1"
                app:layout_constraintEnd_toStartOf="@id/fr_img_delete_entry1"
                android:visibility="visible"
                />
            <ImageView
                android:id="@+id/fr_img_delete_entry7"
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:src="@drawable/ic_remove_circle_24"
                android:layout_marginEnd="8dp"
                android:contentDescription="@string/contentDescriptionDeleteListItem"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="@id/fr_et_list_entry7"
                app:layout_constraintBottom_toBottomOf="@id/fr_et_list_entry7"
                android:visibility="visible"
                />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.core.widget.nestedScrollView>
    <LinearLayout
        android:id="@+id/fr_layout_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:gravity="end"
        android:orientation="horizontal">

        <Button
            android:id="@+id/fr_btn_cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="?android:attr/buttonbarButtonStyle"
            android:text="@android:string/cancel"
            android:textSize="@dimen/textSize_18"
            android:textColor="?attr/colorAccent"
            android:background="@android:color/transparent"
            />
        <Button
            android:id="@+id/fr_btn_ok"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="?android:attr/buttonbarButtonStyle"
            android:text="@android:string/ok"
            android:textSize="@dimen/textSize_18"
            android:textColor="?attr/colorAccent"
            android:background="@android:color/transparent"
            />
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

DialogFragment.java

public class TestDialogFragment extends DialogFragment {

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setView(R.layout.dialog_add_edit_list);
        return builder.create();
    }
}

image from Android Studio

image from emulator

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?