Android 状态栏会将其更改为';全屏对话框内的s颜色变为黑色

Android 状态栏会将其更改为';全屏对话框内的s颜色变为黑色,android,colors,fragment,android-dialogfragment,android-statusbar,Android,Colors,Fragment,Android Dialogfragment,Android Statusbar,我正在使用对话片段。问题是状态栏颜色变为黑色。如何把它换成其他颜色?奇怪的是碎片内部的原因,它工作正常。里面只有黑色的碎片 @Override public void onStart() { super.onStart(); //super.onStart() is where dialog.show() is actually called on the underlying dialog, so we have t

我正在使用对话片段。问题是状态栏颜色变为黑色。如何把它换成其他颜色?奇怪的是碎片内部的原因,它工作正常。里面只有黑色的碎片

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

}

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

在res/value-v21/style中添加以下主题


您必须设置
FLAG\u DRAWS\u SYSTEM\u BAR\u BACKGROUNDS
,以指示此窗口负责绘制系统栏的背景

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

伙计们,最好的解决方案是我在这里发布的网站链接

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

首先,在样式文件中创建样式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>
然后重写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);
    }
}

要更改DialogFragment下的状态栏颜色,请在onCreate方法下将下面的样式设置为DialogFragment

比如:

在style.xml中添加此样式

 <style name="FullScreenDialogWithStatusBarColorAccent">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/white</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:statusBarColor">@color/colorAccent</item>


    <!--<item name="android:windowAnimationStyle">@style/MateriDocumentAlertsActivityalDialogSheetAnimation</item>-->
</style>

真的
@android:彩色/白色
真的
@颜色/颜色重音

以下是我找到的解决方案:

将其添加到style.xml文件中

<style name="BottomSheetDialogTheme" parent="BaseBottomSheetDialog">
    <item name="android:statusBarColor">@android:color/transparent</item>
</style>

<!-- set the rounded drawable as background to your bottom sheet -->
<style name="BottomSheet" parent="@style/Widget.Design.BottomSheet.Modal">
    <item name="android:background">@color/transparent</item>
</style>

<style name="BaseBottomSheetDialog" parent="@style/Theme.Design.Light.BottomSheetDialog">
    <item name="android:windowIsFloating">false</item>
    <item name="bottomSheetStyle">@style/BottomSheet</item>
</style>

在科特林

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        window?.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window?.statusBarColor = Color.parseColor("#0173B7")
    }

此解决方案所需的最小sdk为19。
DialogTheme
可以放入
res/value-v19/style
中,但当您设置
lp.height=WindowManager.LayoutParams.MATCH\u PARENT然后
getWindow().setAttributes(lp)在对话框中。很遗憾,这不是理想的解决方案。这使我能够在显示对话框时设置状态栏的颜色,但在透明度的顶部绘制。要将颜色变暗到与对话框相同的透明度级别可能很棘手。
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setStyle(DialogFragment.STYLE_NO_TITLE,R.style.FullScreenDialogWithStatusBarColorAccent)
}
 <style name="FullScreenDialogWithStatusBarColorAccent">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/white</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:statusBarColor">@color/colorAccent</item>


    <!--<item name="android:windowAnimationStyle">@style/MateriDocumentAlertsActivityalDialogSheetAnimation</item>-->
</style>
<style name="BottomSheetDialogTheme" parent="BaseBottomSheetDialog">
    <item name="android:statusBarColor">@android:color/transparent</item>
</style>

<!-- set the rounded drawable as background to your bottom sheet -->
<style name="BottomSheet" parent="@style/Widget.Design.BottomSheet.Modal">
    <item name="android:background">@color/transparent</item>
</style>

<style name="BaseBottomSheetDialog" parent="@style/Theme.Design.Light.BottomSheetDialog">
    <item name="android:windowIsFloating">false</item>
    <item name="bottomSheetStyle">@style/BottomSheet</item>
</style>
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setStyle(BottomSheetDialogFragment.STYLE_NORMAL, R.style.BottomSheetDialogTheme);

}
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        window?.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window?.statusBarColor = Color.parseColor("#0173B7")
    }