如何使用对话框片段设置全屏对话框

How to set Full screen dialog with dialog fragment

我正在用对话框片段制作全屏对话框。我已成功显示对话框,但问题在于其宽度和高度。它无法显示全屏尺寸。我想通过累积全屏尺寸来设置宽度和高度,如 Activity。请指导我如何设置对话框的宽度和高度以将其累积到全屏大小。

CustomeDialogFramgnet:

public class CustomDialogFragment extends DialogFragment {
    /** The system calls this to get the DialogFragment's layout, regardless
     of whether it's being displayed as a dialog or an embedded fragment. */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout to use as dialog or embedded fragment
        View rootView = inflater.inflate(R.layout.item_fragment, null, false);

        Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
        toolbar.setTitle("Dialog title");

        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);

        ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeAsUpIndicator(android.R.drawable.ic_menu_close_clear_cancel);
        }

        return rootView;
    }

    /** The system calls this only when creating the layout in a dialog. */
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // The only reason you might override this method when using onCreateView() is
        // to modify any dialog characteristics. For example, the dialog includes a
        // title by default, but your custom layout might not need it. So here you can
        // remove the dialog title, but you must call the superclass to get the Dialog.
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        return dialog;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        menu.clear();
        getActivity().getMenuInflater().inflate(R.menu.alert_dialog_input, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_save) {
            // handle confirmation button click here
            return true;
        } else if (id == android.R.id.home) {
            // handle close button click here
            dismiss();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

item_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/listView"
            android:text="zohaib">
        </TextView>

    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>

创建对话框后设置此行。

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

如果您要创建自定义 DialogFragment,请将此写在 DialogFragmentonCreate 中:

setStyle(DialogFragment.STYLE_NORMAL,android.R.style.Theme_Black_NoTitleBar_Fullscreen);

reference

在您的 style.xml 中创建一个 theme,它从 Theme.AppCompat.Light.DarkActionBar 延伸,如下所示:

<style name="DialogFragmentTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

    <item name="android:paddingRight">0dp</item>
    <item name="android:paddingLeft">0dp</item>
    <item name="android:layout_width">match_parent</item>
    <item name="android:windowNoTitle">true</item>
</style>

并设置您在打开 dialog 时创建的 style,如下所示:

CustomDialogFragment mDialog = new CustomDialogFragment();
...
mDialog.setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogFragmentTheme);
mDialog.show(getSupportFragmentManager(), "DialogFragment");

像这样创建主题并添加到您的对话框中

<style name="ThemeDialog" parent="android:Theme.Holo.Dialog">
    <item name="android:windowMinWidthMajor">100%</item>
    <item name="android:windowMinWidthMinor">100%</item>
</style>

创建对话框后设置此行

getDialog().getWindow()
            .getAttributes().windowAnimations = R.style.ThemeDialog;
    WindowManager.LayoutParams wmlp = getDialog().getWindow().getAttributes();
    wmlp.gravity = Gravity.FILL_HORIZONTAL;
    getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0));
    getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);

在您要扩充布局的 onCreate 中添加此行。

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

在您的 style.xml 中创建以下主题:

<style name="DialogTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:paddingRight">0dp</item>
    <item name="android:paddingLeft">0dp</item>
    <item name="android:layout_width">match_parent</item>
    <item name="android:windowNoTitle">true</item>
</style>

然后在DialogFragment中设置样式

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

找到了一种更安全的方式,而且它似乎比 Android 更传统。 DialogFragment 确实继承自 Fragment。因此,只需像其他片段一样显示该片段即可:

activity.supportFragmentManager
        .beginTransaction()
        .replace(R.id.contentFragment, dialogFragment)
        .addToBackStack(null)
        .commitAllowingStateLoss()

试试这个

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

如果以上方法不起作用,请在对话框片段 onCreate 中再添加一件事

setStyle(DialogFragment.STYLE_NORMAL,android.R.style.Theme_Black_NoTitleBar_Fullscreen);