Android: 如何让dialogFragment全屏

Android: How to have dialogFragment to fullscreen

你好,我试图将主题覆盖到 dialogFragment 以实现全屏,但我想要的全屏是在之前 activity 之上的覆盖,所以当 dialogFragment 打开时,我们仍然可以看到 activity 来自屏幕和 dialogFragment 之间的填充。

这是我用过的全屏样式

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

您可以像这样在代码中手动设置布局参数。希望能帮助到你 ! :).还要检查它 Adjusting size of custom dialog box in android

Window window = myDialog.getWindow();
window.setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

这是我为解决您的问题而想出的解决方案:

@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));
        }
    }
    getDialog().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    getDialog().getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

有一种更改样式和主题的方法。

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

我对其进行了全屏测试,并且在简单的布局下工作正常。

你也可以这样做-

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

  }

FWIW,我不得不设置一个特殊的样式,从@style/Base.Theme.AppCompat.Light 继承下来。否则,none 的支持库字段看起来是正确的。如果你使用@style/Base.Theme.AppCompat.Light.Dialog,它会创建我不想要的浮动外观。

以下解决方案非常适合我。

为片段对话框创建如下样式:

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

创建您的 java class 如下:

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

编码愉快!!!

我已经回答了这个问题

In this thread

希望对您有所帮助:)

如果上述 none 有效(就像它们对我不起作用),请查看 Android Developer Dialogs Guide 以获取有关如何实现此目的的更多信息。

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 仍然显示,you'll need to set some flags on the 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)
}

下面是我的解决方案。

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