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

标签 android android-layout android-dialogfragment

我有一个简单的 DialogFragment,它包含 3 个 EditText 和一个 Button。我已经将其主布局的 layout_width 设置为 350dp 但是当我运行该项目时它会显示得如此纤细。

Screen Shot

我的 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),以保持您的布局响应所有屏幕尺寸。

关于android - 为什么我的 Dialog Fragment 显示得这么薄?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56920460/

相关文章:

java - 如何使用Android Studio生成ProGuard混淆?

android - Espresso 中的 "IdlingResourceTimeoutException"是什么?

android - 如何在 Android 的设置菜单中添加新项目?

android - 从 DialogFragment (DatePicker) 检索和设置数据

android - DialogFragment 中的 ViewPager - IllegalStateException : Fragment does not have a view

android - 跳转到 Android Studio 调试器中的当前可执行行

android - 未调用 SQLiteOpenHelper onCreate 方法

出现时android软键盘破坏布局

android - 如何防止希伯来语和阿拉伯语的自动从右到左文本方向?

android - 如何显示来自处理程序的 DialogFragment