Android 全屏对话框 fragment ,如日历应用程序

标签 android material-design android-dialogfragment android-statusbar

我正在尝试实现如下图所示的全屏对话框。我能够显示全屏对话框,但是当显示对话框时,状态栏颜色变为黑色并且不保持原色深色。

这是我的对话 fragment

public class IconsDialogFragment extends DialogFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        // Inflate the layout to use as dialog or embedded fragment
        return inflater.inflate(R.layout.fragment_icons_dialog, container, false);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        final RelativeLayout root = new RelativeLayout(getActivity());
        root.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        // creating the fullscreen dialog
        final Dialog dialog = new Dialog(getActivity());
        dialog.setContentView(root);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.YELLOW));
        dialog.getWindow().setWindowAnimations(R.style.DialogAnimation);
        dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        return dialog;
    }

}

enter image description here

最佳答案

全屏获取DialogFragment

像这样覆盖 DialogFragment 的 onStart:

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

为了设置 StatusBarColor,您需要设置标志:FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS

public void setStatusBarColorIfPossible(int color) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
   getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
   getWindow().setStatusBarColor(color);
   }
}

关于Android 全屏对话框 fragment ,如日历应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37433448/

相关文章:

android - windowSoftInputMode 在 Android M 中没有效果

Android Intent 打开应用程序不工作

android - dlsym : undefined symbol, 安卓 N

jquery - 新 Material 设计图标主题 - 设置颜色

javascript - 我使用 MDL 得到 2 个滚动条,我该如何修复它?

java - 在 OnClickListener 中实现 DialogFragment 接口(interface)

java - onActivityResult 与 ImageView

android - 如何在 zxing 中触发批量模式扫描

android - Material 主题工具栏上的渐变背景

android - onContextItemSelected 不会在 DialogFragment 中被调用