为什么我的 Dialog Fragment 显示得这么薄?

Why is my Dialog Fragment showing so thin?

我有一个简单的 DialogFragment,其中包含 3 个 EditText 和一个 Button。我已经设置了它的主要布局 layout_width 350dp 但是当我 运行 项目时它会显示得这么苗条。

这是我的 DialogFragment XML:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="350dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:animateLayoutChanges="true"
    android:background="#AFE6FF"
    android:orientation="vertical"
    android:padding="24dp">

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


        <android.support.constraint.ConstraintLayout
            android:id="@+id/layout_mobile"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <EditText
                android:id="@+id/et_mobile"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

        </android.support.constraint.ConstraintLayout>


        <android.support.constraint.ConstraintLayout
            android:id="@+id/layout_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <EditText
                android:id="@+id/et_title"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

        </android.support.constraint.ConstraintLayout>


        <android.support.constraint.ConstraintLayout
            android:id="@+id/layout_body"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/layout_title">

            <EditText
                android:id="@+id/et_body"
                android:layout_width="match_parent"
                android:layout_height="150dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

        </android.support.constraint.ConstraintLayout>


        <Button
            android:id="@+id/btn_send"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Send"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/layout_body" />


    </LinearLayout>

</ScrollView>

它会被这个代码显示:

MyDialogFragment.newInstance().show(getSupportFragmentManager(), "my_dialog_fragment");

我需要它显示得更宽。

您需要像下面的代码一样更改对话框的 LayoutParams。把它放在 onResume():

Window window = yourDialog.getWindow();
if (window != null) {
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    lp.copyFrom(window.getAttributes());
    //This makes the dialog take up the full width
    lp.width = WindowManager.LayoutParams.MATCH_PARENT;
    lp.height = WindowManager.LayoutParams.MATCH_PARENT;
    window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    window.setAttributes(lp);
}

我认为默认对话框的 LayoutParams (wrap_content,wrap_content) 覆盖了您在 XML 中的定义。

最好以这种方式使用它,而不是使用固定大小值 (350dp),以保持您的布局响应所有屏幕尺寸。

您想覆盖 onCreateDialog 并在那里添加您的自定义布局。如下图。我使用 ViewBinding 创建我的视图。

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return AlertDialog.Builder(requireContext())
            .setView(binding.root)
            .setCancelable(false)
            .create().apply {
                setCanceledOnTouchOutside(false)
            }
    }