如何将 DialogFragment 的宽度设置为 Fill_Parent

118

我正在开发一个Android应用程序,在其中使用DialogFragment来显示对话框,但其宽度非常小。我应该如何将其宽度设置为fill_parent

public class AddNoteDialogFragment extends DialogFragment {

    public AddNoteDialogFragment() {
        // Empty constructor required for DialogFragment
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        getDialog().setTitle(getString(R.string.app_name));
        View view = inflater.inflate(R.layout.fragment_add_note_dialog,
                container);

        return view;
    }


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);

        // request a window without the title
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);

        return dialog;
    }
}

fragment_add_note_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <EditText
        android:id="@+id/addNoteEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="top"
        android:hint="@string/clock_enter_add_note"
        android:imeOptions="actionDone"
        android:inputType="textCapSentences|textMultiLine"
        android:lines="5" />

    <Button
        android:id="@+id/submit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@drawable/login_button"
        android:text="@string/submit_button"
        android:textColor="@android:color/white" />

</LinearLayout>


3
实际上,使用RelativeLayout 实现相同的布局效果很好。我不解释这个... - Dan Chaltiel
尝试这个答案 - Kotlin(2022) - https://dev59.com/8V8d5IYBdhLWcg3wTQsn#72570746 - Muhammed Ashraf
21个回答

180

这是我想出来应对此问题的解决方案:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = super.onCreateDialog(savedInstanceState);    
    dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);

    return dialog;
}

@Override
public void onStart() {
    super.onStart();

    Dialog dialog = getDialog();
    if (dialog != null) {
       dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
       dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }
}

编辑:

在返回充气的视图之前,在片段的onCreateView方法中可以使用下面的代码。

getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

3
您只需要调用 dialog.getWindow().setBackgroundDrawable(null); 即可。 - xuxu
如果将其设置为null,则不会透明...只有在4.0.4上是黑色的。 - Joao Polo
21
如果我将getDialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);放在DialogFragment的onResume()方法中,它对我很有效。但是在onCreateView()方法中它无法正常工作。我稍微修改了答案,从dialog改为了getDialog() - Rock Lee
2
onStart方法中的setLayout工作得非常完美。谢谢。 - wonsuc

97

你尝试过使用 Elvis 在 如何使警告对话框填充屏幕大小 中的答案吗?

它是以下内容:

dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

更新:

以上代码应该添加到DialogFragmentonStart()方法中。


7
你导入了哪个LayoutParams? - ymerdrengene
3
@ymerdrengene 很好的问题。尝试使用WindowManager.LayoutParams.FILL_PARENT - EpicPandaForce
2
@ymerdrengene 尽管从技术上讲,它们都只是从 ViewGroup.LayoutParams 继承了这个值,所以无论哪种方式都可以完美地工作。 - EpicPandaForce
1
如此提到:https://dev59.com/3GUp5IYBdhLWcg3wbnMS#15279400 我建议将这段代码移动到 onCreateDialog - Ishan
2
导入 android.view.WindowManager; - kirti
显示剩余2条评论

68

这对我起作用了

创建您自定义的样式:

   <style name="DialogStyle" parent="Base.Theme.AppCompat.Dialog">
    <item name="android:windowMinWidthMajor">97%</item>
    <item name="android:windowMinWidthMinor">97%</item>
   </style>

你也可以尝试使用正确的父级来匹配其他对话框,例如 parent="ThemeOverlay.AppCompat.Dialog"等等。

在你的对话框中使用这种样式。

public class MessageDialog extends DialogFragment {

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogStyle);
}

// your code 
}

5
使用<item name="android:windowBackground">@null</item>是一个简洁而优雅的解决方案。 - Evgeny Sinitsky
3
请注意使用正确的父级以匹配您的其他对话框。在我的情况下,它是ThemeOverlay.AppCompat.Dialog。 - quealegriamasalegre

40

对我来说,在onCreateView中填充的布局LinearLayout的父容器替换为RelativeLayout后,它就有效了。不需要进行其他代码更改。


11
典型的情况,总是在Stack Overflow上最不受欢迎的答案解决了我的问题。谢谢。 - busuu
1
我非常喜欢这个答案,但不知道为什么它只适用于相对布局? - Prateek Gupta

22
    <!-- A blank view to force the layout to be full-width -->
    <View
        android:layout_width="wrap_content"
        android:layout_height="1dp"
        android:layout_gravity="fill_horizontal" />

在我的对话框布局的顶部就解决了问题。


3
这对我很有效,我喜欢它不需要我们在代码中添加任何额外的东西,但我想知道为什么它有效... - shela
令人惊讶的是,这是我唯一有效的方法。自定义样式不起作用。 - Magnus
完美。需要像这样简单的东西。 - Denny Mathew
你的根布局是什么? - Mattwmaster58

20
public class FullWidthDialogFragment extends AppCompatDialogFragment {
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.Theme_AppCompat_Light_Dialog_Alert);
    }
}

如果你想要更多的灵活性,可以扩展AppCompat_Dialog_Alert并自定义属性。


18

基于其他解决方案,我创建了自己的解决方案。

<style name="AppTheme.Dialog.Custom" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowMinWidthMajor">100%</item>
    <item name="android:windowMinWidthMinor">100%</item>
</style>
abstract class BaseDialogFragment : DialogFragment() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.AppTheme_Dialog_Custom)
    }
}

12

我想澄清这一点。两种方式都是正确的,但使用不同的DialogFragment。

使用android.app.DialogFragment

@Override
public void onStart()
{
    super.onStart();
    Dialog dialog = getDialog();
    if (dialog != null)
    {
        int width = ViewGroup.LayoutParams.MATCH_PARENT;
        int height = ViewGroup.LayoutParams.MATCH_PARENT;
        dialog.getWindow().setLayout(width, height);
    }
}

使用 android.support.v4.app.DialogFragment

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
}

基本上我需要全宽,这个很有效。谢谢! - sud007

12

在我的情况下,我也采用了以下方法:

    @Override
    public void onStart() {
        super.onStart();
        getDialog().getWindow().setLayout(LayoutParams.MATCH_PARENT, (int) getResources().getDimension(R.dimen.dialog_height));
    }
}

但是在一些Lollipop+设备上(例如Nexus 9),对话框的左右边缘与屏幕边缘之间仍然存在小间隙

虽然并不明显,但最终我发现要使其在所有设备和平台上都可以实现全宽度窗口背景应该在styles.xml中指定,如下所示:

<style name="Dialog.NoTitle" parent="Theme.AppCompat.Dialog">
    <item name="android:windowNoTitle">true</item>
    <item name="android:padding">0dp</item>
    <item name="android:windowBackground">@color/window_bg</item>
</style>

当我们创建以下对话框时,当然需要使用这种样式:

    public static DialogFragment createNoTitleDlg() {
        DialogFragment frag = new Some_Dialog_Frag();
        frag.setStyle(DialogFragment.STYLE_NO_TITLE, R.style.Dialog_NoTitle);
        return frag;
}

2
这应该是被接受的答案!节省了我很多时间!窗口背景起了作用。 - Karan

4

这对我来说完美无缺。

android:minWidth="300dp"

通过这种方式,您可以为对话框片段指定宽度。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接