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

Android 如何使用对话框片段设置全屏对话框,android,android-dialogfragment,Android,Android Dialogfragment,我正在用对话片段制作全屏对话。我成功地显示了这个对话框,但问题在于它的宽度和高度。它不能显示全屏大小。我想通过像活动一样累积全屏大小来设置宽度和高度。请指导我如何设置对话框的宽度和高度,以累积到全屏大小 CustomeDialogFramgnet: public class CustomDialogFragment extends DialogFragment { /** The system calls this to get the DialogFragment's layout,

我正在用对话片段制作全屏对话。我成功地显示了这个对话框,但问题在于它的宽度和高度。它不能显示全屏大小。我想通过像活动一样累积全屏大小来设置宽度和高度。请指导我如何设置对话框的宽度和高度,以累积到全屏大小

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
,请在您的
DialogFragment
onCreate
中写入此命令:

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

style.xml
中创建一个
主题,该主题从
主题.AppCompat.Light.darkaActionBar
扩展而来,如下所示:

<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>
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>

将这一行添加到正在膨胀布局的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>

找到了一种更安全的方式,这对Android来说似乎很传统。DialogFragment确实继承自Fragment。因此,只需像其他片段一样显示片段:

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

在使用协调器布局之后,可以在对话框片段中使用底部栏。。您可以拖动整个屏幕,请编写一些示例代码。请参考此示例。。它可能会帮助你。。。小的主题详细信息-在创建视图的
中缺少
设置选项菜单(true)
。如果没有它,菜单将不会呈现,当然也永远不会调用选项ItemSelected;但是你还有其他方法吗?太好了,祝你有愉快的一天。还有一个问题,dimis()函数也不起作用。你知道吗?它应该工作得很理想,你能把代码发布到你调用它的地方吗?@Override public boolean onOptionsItemSelected(MenuItem item){int id=item.getItemId();if(id==R.id.action_save){//句柄确认按钮单击此处返回true;}否则如果(id==android.R.id.home){//handle close按钮单击此处dismise();返回true;}返回super.onoptionItemSelected(item);}在这里,您试图将片段的
工具栏
视图设置为
活动
操作栏
,我认为它不是这样工作的,您的菜单点击android.R.id.home
不能被调用,因为您没有点击
活动
主页
按钮。相反,您可以简单地保留将
主页
按钮作为片段的
工具栏
内的视图,并使用
onclick listener
对其进行处理。将重力设置为水平填充是我一直在寻找的。谢谢。
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogTheme);
}
activity.supportFragmentManager
        .beginTransaction()
        .replace(R.id.contentFragment, dialogFragment)
        .addToBackStack(null)
        .commitAllowingStateLoss()