状态栏在全屏对话框片段android中将其颜色更改为黑色

问题描述 投票:3回答:3

我正在使用对话框片段。问题是状态栏颜色变为黑色。如何将其改为其他颜色?内部碎片很奇怪,活动很好。它内部只有黑色的DialogFragment

        @Override
            public void onStart() {
                super.onStart();    //super.onStart() is where dialog.show() is actually called on the underlying dialog, so we have to do it after this point
                Dialog d = getDialog();
                if (d != null) {

                    int width = ViewGroup.LayoutParams.MATCH_PARENT;
                    int height = ViewGroup.LayoutParams.MATCH_PARENT;
                    d.getWindow().setLayout(width, height);
                    d.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

                }
            }
     @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            final Dialog dialog = new Dialog(getActivity(), R.style.full_screen_dialog);
            return dialog;

}
android colors fragment android-dialogfragment android-statusbar
3个回答
4
投票

我刚刚发布了这个问题的解决方案here

将以下主题添加到res / value-v21 / style

<style name="DialogTheme" parent="@style/Base.Theme.AppCompat.Light.Dialog">
     <item name="android:windowTranslucentStatus">true</item>
</style>

然后在DialogFragmentonCreate上应用Style

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogTheme);
}

9
投票

你需要设置FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDSto表示此窗口负责绘制系统栏的背景。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        dialog.getWindow().setStatusBarColor(yourColor); 
    }

3
投票

GUYS最好的解决方案是我在这里发布的网站链接

https://zocada.com/android-full-screen-dialogs-using-dialogfragment/

我也会在这里上传代码以供参考

首先在样式文件中创建样式FullScreenDialogStyle:

<style name="FullScreenDialogStyle" parent="Theme.AppCompat.Dialog">
    <item name="android:windowNoTitle">true</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorPrimary">@color/colorPrimary</item>

    <!-- Set this to true if you want Full Screen without status bar -->
    <item name="android:windowFullscreen">false</item>

    <item name="android:windowIsFloating">false</item>

    <!-- This is important! Don't forget to set window background -->
    <item name="android:windowBackground">@color/colorWhite</item>

    <!-- Additionally if you want animations when dialog opening -->
    <!--<item name="android:windowEnterAnimation">@anim/slide_up</item>-->
    <!--<item name="android:windowExitAnimation">@anim/slide_down</item>-->
</style>

在dialogFragment类中覆盖onCreate方法

 @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL,R.style.FullScreenDialogStyle);

    }

然后重写Onstart方法,通过它可以访问getDialog()并设置高度和宽度

@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);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.