简体   繁体   English

如何在不重叠状态栏/导航栏和状态栏颜色的情况下使DialogFragment全屏显示?

[英]How to make DialogFragment fullscreen without overlapping status bar/navigation bar and mantain status bar color?

I've been googling for hours and tried countless things but I just can't get this to work... 我已经搜寻了好几个小时,尝试了无数次尝试,但我无法解决这个问题……

This is MyFragment.java : 这是MyFragment.java

public class MyFragment extends DialogFragment {

    public MyFragment() {
        // Required empty public constructor
    }

    public static MyFragment newInstance() {
        MyFragment newFragment = new LoginDialogFragment();
        newFragment.setCancelable(false);
        return newFragment;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new Dialog(getActivity(), R.style.MyAppTheme_Dialog);
    }

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

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        Window window = getDialog().getWindow();

        if (window != null) {
            window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
            window.setWindowAnimations(R.style.MyAppTheme_Dialog);
        }
    }

}

And this is styles.xml : 这是styles.xml

<style name="MyAppTheme" parent="PreferenceFixTheme.Light.NoActionBar">
    <item name="android:windowBackground">@color/background</item>
    <item name="android:colorBackground">@color/background</item>
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/accent</item>
    <item name="preferenceCategory_marginBottom">0dp</item>
</style>

<style name="MyAppTheme.Dialog">
    <item name="android:backgroundDimEnabled">false</item>
    <item name="android:windowBackground">@null</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowEnterAnimation">@anim/slide_up</item>
    <item name="android:windowExitAnimation">@anim/slide_down</item>
    <item name="android:windowFrame">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
</style>

This almost works as I expect it... The only problem is that calling window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT); 这几乎可以按我期望的那样工作...唯一的问题是调用window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT); to fill the whole screen (minus the status/navigation bars) changes the status bar color to black. 填充整个屏幕(减去状态/导航栏)会将状态栏颜色更改为黑色。 When I close the dialog, it changes back to colorPrimaryDark . 当我关闭对话框时,它变回colorPrimaryDark

If I remove that window.setLayout(...) line, the status bar will keep the colorPrimaryDark but the dialog size will be the default (a small square on the middle of the screen). 如果删除window.setLayout(...)行,状态栏将保留colorPrimaryDark但对话框大小将为默认大小(屏幕中间的小方块)。

In the same place I call window.setLayout(...) , I tried putting the following code: 在同一位置,我调用window.setLayout(...) ,我尝试放置以下代码:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    getWindow().setStatusBarColor(ContextCompat.getColor(getActivity, R.color.primary_dark));
}

This sets the status bar color to colorPrimaryDark but it overlaps both the status and navigation bar. colorPrimaryDark状态栏颜色设置为colorPrimaryDark但与状态栏和导航栏重叠。

I just can't get fullscreen without overlapping the status and navigation bars and keep colorPrimaryDark on the status bar. 如果不重叠状态栏和导航栏,并在状态栏上保留colorPrimaryDark ,就无法获得全屏显示。

A few questions I've found with the same problem but I couldn't get the suggestions to work. 我发现了与同一问题有关的几个问题,但我无法获得建议。 Neither have an accepted answer: 都没有一个可接受的答案:

check this customized dialog fragment, try it which will satisfy your need . 检查此自定义对话框片段,然后尝试满足您的需求。 and let me know. 让我知道

 import android.content.pm.ActivityInfo;
 import android.content.res.Configuration;
 import android.graphics.Point;
 import android.os.Build;
 import android.os.Bundle;
 import android.support.v4.app.DialogFragment;
 import android.util.TypedValue;
 import android.view.Display;

/**
  * Created by Noorul on 02-07-16.
*/

public class _DialogFragment extends DialogFragment {
  @Override
  public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
  //   preventScreenRotation();
  }

  @Override
  public void onDestroy() {
    // releaseScreenRotation();
    super.onDestroy();
}

@SuppressWarnings("deprecation")
@Override
public void onStart() {
    super.onStart();

    // change dialog width
    if (getDialog() != null) {

        int fullWidth = getDialog().getWindow().getAttributes().width;
        int fullHeight = getDialog().getWindow().getAttributes().height;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
            Display display =    getActivity().getWindowManager().getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);

            fullWidth = size.x;
            fullHeight = size.y;
        } else {
            Display display = getActivity().getWindowManager().getDefaultDisplay();
            fullWidth = display.getWidth();
            fullHeight = display.getHeight();
        }

        final int padding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 0, getResources()
                .getDisplayMetrics());

        int w = fullWidth - padding;
        int h = fullHeight - (padding * 2);

        getDialog().getWindow().setLayout(w, h);
    }
}


 private void preventScreenRotation() {
     if (getResources().getConfiguration().orientation ==  Configuration.ORIENTATION_PORTRAIT) {
                getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
     } else {  
      getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
  }

  private void releaseScreenRotation() {
      getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
}

change the size of the dialog depends on your need. 更改对话框的大小取决于您的需要。

To create a fullscreen dialog according to Material design guidelines. 根据材料设计准则创建全屏对话框。 Put this code in the DialogFragment: 将此代码放在DialogFragment中:

 @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        return dialog;
    }

Call this dialog by: 通过以下方式调用此对话框:

getSupportFragmentManager().beginTransaction().add(android.R.id.content, FullScreenDialog.newInstance()).addToBackStack(null).commit();

Set the top margin to 24dp in the base layout. 在基本布局中将顶部边距设置为24dp。 So that the dialog doesn't overlap the statusbar. 这样对话框就不会与状态栏重叠。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM