Android:如何将dialogFragment设置为全屏

时间:2015-10-14 00:26:38

标签: android fullscreen android-dialogfragment

您好我已经尝试将主题覆盖到dialogFragment全屏,但我想要的全屏是上一个活动的叠加层,所以当打开dialogFragment时,我们仍然可以看到屏幕之间填充的活动和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>

10 个答案:

答案 0 :(得分:41)

这是我想出来处理你的问题的解决方案:

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

答案 1 :(得分:13)

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

为Fragment Dialog创建样式,如下所示:

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

按如下所示创建您的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;
    }
}

快乐的编码!!!

答案 2 :(得分:7)

你也可以做类似的事情 -

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

  }

答案 3 :(得分: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>

答案 4 :(得分:1)

您可以在代码中手动设置布局参数。希望能帮助到你 ! :)。另请检查此SO Adjusting size of custom dialog box in android

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

答案 5 :(得分:1)

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

答案 6 :(得分:1)

有一种方法可以改变风格和主题。

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

我测试了它以获得全屏,并且它的工作正常,布局简单。

答案 7 :(得分:0)

FWIW,我必须设置一个特殊的样式,它继承了@ style / Base.Theme.AppCompat.Light。否则,没有任何支持库字段看起来正确。如果您使用@ style / Base.Theme.AppCompat.Light.Dialog,它会创建我不想要的浮动外观。

答案 8 :(得分:0)

我已经解释了这个问题

In this thread

希望这会有所帮助:)

答案 9 :(得分:0)

如果以上方法均无效(例如它们不适合我),请查看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();
    }
}

如果像我一样,在使用上述策略you'll need to set some flags on the UI之后,您的ActionBar仍然显示。

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