【问题标题】:How to set Full screen dialog with dialog fragment如何使用对话框片段设置全屏对话框
【发布时间】:2017-01-06 07:21:56
【问题描述】:

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

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>

【问题讨论】:

  • 您可以在对话框片段中使用底部栏,毕竟您使用的是协调器布局..您可以全屏拖动
  • 请写一些示例代码
  • 推荐这个..它可能对你有帮助...androidhive.info/2016/04/…
  • 小的离题细节 - onCreateView 中缺少 setHasOptionsMenu(true)。没有它,菜单将不会呈现,当然onOptionsItemSelected 将永远不会被调用。

标签: android android-dialogfragment


【解决方案1】:

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

<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");

【讨论】:

  • 太好了,祝你有美好的一天。
  • 还有一个问题,dimiss() 函数也不起作用。你有什么想法吗?
  • 它应该可以正常工作,您可以将代码发布在您调用它的位置吗?
  • @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_save) { // 处理确认按钮点击这里 return true; } else if (id == android.R.id.home) { // 处理关闭按钮点击这里dismiss();返回真; } 返回 super.onOptionsItemSelected(item); }
  • 在这里,您尝试将片段的 toolbar 视图设置为您的 activityactionbar,我认为它不会那样工作,并且您的菜单单击 android.R.id.home不得调用,因为您没有单击activityhome 按钮。相反,您可以简单地将 home 按钮保留为片段的 toolbar 内的视图,并使用 onclick listener 处理它。
【解决方案2】:

在创建对话框后设置这一行。

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

【讨论】:

  • 我在这行之后添加了 Dialog dialog = super.onCreateDialog(savedInstanceState);但不工作
  • 您还有其他方法吗?
【解决方案3】:

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

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

【讨论】:

  • 将重力设置为 FILL_HORIZONTAL 是我一直在寻找的东西。谢谢。
【解决方案4】:

在您的 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);
}

【讨论】:

    【解决方案5】:

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

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

    【讨论】:

      【解决方案6】:

      试试这个

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

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

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

      【讨论】:

        【解决方案7】:

        在 onCreate 中添加这一行,以增加布局。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-09-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-05-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多