Android:如何将dialogFragment设置为全屏显示

14

你好,我尝试覆盖对话框片段的主题以实现全屏,但我想要的全屏是一个覆盖在之前活动之上的叠加层,因此当打开对话框片段时,我们仍然可以从屏幕和对话框片段之间的填充看到后面的活动。

这是我用于全屏的样式

<style name="fullscreen_dialog" parent="android:Theme" >
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowIsFloating">false</item>
</style>

10个回答

51

这是我想出来用来解决您问题的方法:

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

18

以下解决方案完美地适用于我。

为 Fragment 对话框创建如下样式:

<style name="dialog_theme" parent="android:Theme" >
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">false</item>
</style>
创建如下Java类:

Create your java class as below:

->

创建以下Java类:

public class FiltersDialogFragment extends android.support.v4.app.DialogFragment {

    static FiltersDialogFragment newInstance() {
        FiltersDialogFragment fragment = new FiltersDialogFragment();
        return fragment;
    }

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

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_filters, container, false);
        return view;
    }
}

快乐编程!!!


工作完美。干得好。 - KingWu

10

你也可以做类似这样的事情 -

@Override
  public void onCreate(@Nullable final Bundle savedInstanceState) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      setStyle(STYLE_NO_TITLE, android.R.style.Theme_Material_Light_NoActionBar_Fullscreen);
    } else {
      setStyle(STYLE_NO_TITLE, android.R.style.Theme_DeviceDefault_Light_NoActionBar);
    }
    super.onCreate(savedInstanceState);

  }

1
谢谢,这似乎是唯一对我有效的方法,与其他人提到的在onStart中使用MATCH_PARENT一起。 - Aranda

3
以下是我的解决方案。
<style name="Dialog.App" parent="Theme.AppCompat.Dialog"></style>
<style name="Dialog.App.Fullscreen">
    <item name="android:windowActionBar">false</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:colorBackground">@null</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
    <item name="android:windowFrame">@null</item>
    <item name="android:windowBackground">@null</item>

    <!--the key attribute for fullscreen dialog -->
    <item name="android:windowIsFloating">false</item>

    <!-- only width fill screen -->
    <!--<item name="android:windowMinWidthMajor">100%</item>-->
    <!--<item name="android:windowMinWidthMinor">100%</item>-->
</style>

1
    getDialog().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    getDialog().getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

1
你可以在代码中手动设置布局参数,如下所示。希望能有所帮助! :)。还可以查看这个SO的链接调整Android中自定义对话框框的大小
Window window = myDialog.getWindow();
window.setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

1
此答案使用了已弃用的代码,请立即选择其他解决方案。现在更多地接受使用对话框主题。 - Cory Roy

0

顺便说一下,我不得不设置一个特殊的样式,它是从@style/Base.Theme.AppCompat.Light子类化而来的。否则,支持库字段都看起来不对。如果你使用@style/Base.Theme.AppCompat.Light.Dialog,它会创建那种浮动的外观,而我并不想要。


0

我已经回答了这个问题并附上了解释。

在这个帖子中

希望这能有所帮助 :)


0

如果以上方法都不起作用(就像对我来说一样),请参考Android开发者对话框指南,获取更多有关如何实现此功能的信息。

public void showDialog() {
    FragmentManager fragmentManager = getSupportFragmentManager();
    CustomDialogFragment newFragment = new CustomDialogFragment();

    if (mIsLargeLayout) {
        // The device is using a large layout, so show the fragment as a dialog
        newFragment.show(fragmentManager, "dialog");
    } else {
        // The device is smaller, so show the fragment fullscreen
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        // For a little polish, specify a transition animation
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        // To make it fullscreen, use the 'content' root view as the container
        // for the fragment, which is always the root view for the activity
        transaction.add(android.R.id.content, newFragment)
                   .addToBackStack(null).commit();
    }
}

如果像我一样,使用上述策略后ActionBar仍然显示,您需要在UI上设置一些标志

private fun hideSystemUI() {
    // Enables regular immersive mode.
    // For "lean back" mode, remove SYSTEM_UI_FLAG_IMMERSIVE.
    // Or for "sticky immersive," replace it with SYSTEM_UI_FLAG_IMMERSIVE_STICKY
    window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE
            // Set the content to appear under the system bars so that the
            // content doesn't resize when the system bars hide and show.
            or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            // Hide the nav bar and status bar
            or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            or View.SYSTEM_UI_FLAG_FULLSCREEN)
}

0

有一种方法可以改变样式和主题。

/* theme is optional, I am using leanback... */
setStyle(STYLE_NORMAL, R.style.AppTheme_Leanback);

我测试过简单布局,全屏工作得非常好。


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