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

the status bar changes it's color to black inside fullscreen dialog fragment android

我正在使用对话框片段。问题是状态栏颜色更改为黑色。如何将其更改为其他颜色?这很奇怪,因为片段内部 activity 它工作正常。它在 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;

}

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

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

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

然后在 onCreate

中的 DialogFragment 上应用样式
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogTheme);
}

你必须设置FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS来表明这个Window负责绘制系统栏的背景。

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

伙计们最好的解决方案在网站上LINK我在这里发帖

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

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

1st 在样式文件中创建样式 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 中 class 覆盖 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);
    }
}

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

赞:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setStyle(DialogFragment.STYLE_NO_TITLE,R.style.FullScreenDialogWithStatusBarColorAccent)
}

在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>

这是我找到的解决方案:

将此添加到您的 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>

那么你应该覆盖 BottomSheetDialogFragment class 中的 onCreate() 方法并调用:

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

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

}

在 kotlin 中

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

派对有点晚了,但设置 IS_FLOATING 标志对我有用。

<style name="MyTheme.AlertDialog.FullScreen" parent="MyTheme.AlertDialog">
    <item name="windowNoTitle">true</item>
    <item name="android:windowIsFloating">false</item>
    <item name="android:windowAnimationStyle">@style/Animation.AppCompat.Dialog</item>
    <item name="android:windowBackground">@color/white</item>
</style>