如何使DialogFragment全屏不重叠状态栏/导航栏和mantain状态栏颜色?

时间:2016-12-10 04:02:37

标签: java android android-dialogfragment android-statusbar

我已经谷歌搜索了几个小时,并尝试了无数的东西,但我不能让它工作......

这是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); } } }

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>

window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);

这几乎与我期望的一样......唯一的问题是调用colorPrimaryDark来填充整个屏幕(减去状态/导航栏)会将状态栏颜色更改为黑色。当我关闭对话框时,它会变回window.setLayout(...)

如果我删除了colorPrimaryDark行,状态栏将保留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)); } 的同一个地方,我尝试输入以下代码:

colorPrimaryDark

这会将状态栏颜色设置为colorPrimaryDark,但它会与状态和导航栏重叠。

我无法在不重叠状态和导航栏的情况下获得全屏,并在状态栏上保留a:0:{}

我发现了一些问题同样的问题,但我无法得到建议。没有一个接受的答案:

2 个答案:

答案 0 :(得分:2)

检查此自定义对话框片段,尝试它以满足您的需要。让我知道。

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

根据需要更改对话框的大小。

答案 1 :(得分:0)

根据材料设计指南创建全屏对话框。 将此代码放在DialogFragment中:

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

通过以下方式调用此对话框:

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

在基本布局中将上边距设置为24dp。这样对话框就不会与状态栏重叠。