当primaryDark为白色时,如何更改状态栏文本颜色

138

我试图复制谷歌日历应用程序的行为:

这里输入图片描述

但是我还没有找到一种改变状态栏文字颜色的方法。如果我将colorPrimaryDark设置为白色,由于它们的颜色也是白色,我既看不到状态栏中的图标也看不到文字。

有没有办法改变状态栏文字颜色?

16个回答

269

我不确定您要针对哪个API级别进行目标,但如果您可以使用API 23特定的内容,您可以将以下内容添加到您的AppTheme styles.xml中:

<item name="android:statusBarColor">@color/colorPrimaryDark</item>
<item name="android:windowLightStatusBar">true</item>

android:windowLightStatusBar 设置为 true 时,状态栏文本颜色将在状态栏颜色为白色时可见,反之亦然。当 android:windowLightStatusBar 设置为 false 时,状态栏文本颜色将被设计成在状态栏颜色为深色时可见。

示例:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <!-- Status bar stuff. -->
    <item name="android:statusBarColor">@color/colorPrimaryDark</item>
    <item name="android:windowLightStatusBar">true</item> 
</style>

46
如果我的API级别为21而它不起作用,你能告诉我替代方案吗? - Bala Saikrupa Puram
9
找到了适用于Api 21的解决方案。请帮忙吗? - iMDroid
4
注意:android:windowLightStatusBar 需要 API 级别 23(当前最低级别为 21)更多信息...(⌘F1) - Hardik Darji
4
请查看 API 等级小于 23 的此帖子:https://dev59.com/Hanka4cB1Zd3GeqPTMOf#49255140 - Basti Vagabond
@BastiVagabond 这不是解决方案,它只是抑制了警告。在 API < 23 上仍然无法工作。 - Farid
显示剩余3条评论

39

您可以通过编程来完成这个操作,像这样 答案

只需添加此内容

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

13
setSystemUiVisibility方法已过时。 - Luca Ziegler

33

兼容版本适用于API 23及以上版本

这是它的代码:

// deprecated
// WindowInsetsControllerCompat(window, view).isAppearanceLightStatusBars = Boolean
// also deprecated
// ViewCompat.getWindowInsetsController(view)?.isAppearanceLightStatusBars = Boolean
WindowCompat.getInsetsController(window, decorView)?.isAppearanceLightStatusBars = Boolean

您可以直接从Activity获取window

我喜欢将其添加到Window扩展方法中:

fun Window.setLightStatusBars(b: Boolean) {
    WindowCompat.getInsetsController(this, decorView)?.isAppearanceLightStatusBars = b
}

你需要androidx.core来实现此功能


Ace -> 很好!谢谢! - AlexS
此方法不影响API <23。https://developer.android.com/reference/androidx/core/view/WindowInsetsControllerCompat#setAppearanceLightStatusBars(boolean) - Andy

28

非常简单:

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);//  set status text dark
getWindow().setStatusBarColor(ContextCompat.getColor(BookReaderActivity.this,R.color.white));// set status background white

反之亦然:

getWindow().setStatusBarColor(ContextCompat.getColor(BookReaderActivity.this, R.color.black));
View decorView = getWindow().getDecorView(); //set status background black 
decorView.setSystemUiVisibility(decorView.getSystemUiVisibility() & ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); //set status text  light

17

跟随 @Jon 的回答,我会在新的API上稍微更新一下。对于新的API和主题以及夜间模式(暗黑模式),我会通过添加 v23/styles.xml 并在那里设置状态栏背景和文字颜色来实现:

<item name="android:statusBarColor">@color/lightColor</item>
<item name="android:windowLightStatusBar">true</item>

而在night/styles.xml文件中:

<item name="android:statusBarColor" tools:targetApi="l">@color/darkColor</item>
<item name="android:windowLightStatusBar" tools:targetApi="m">false</item>

默认的styles.xml不会包含任何此代码,或仅包含此代码,但记得不要将其设置为light:

<item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
这样,我们为状态栏设置了浅色背景(和文本颜色),但仅适用于API版本为23及以上的设备。在API版本小于23的设备上,背景将不会更改,因为我认为这不是我们想要的,而文本颜色将保持为白色。 暗色主题在API 29中添加,因此我们不必担心API 21上的暗色主题 ;) 然而,缺点是我们又添加了一个需要记住管理的文件。

4

和之前一样,SYSTEM_UI_FLAG_LIGHT_STATUS_BAR 在我的情况下起到了作用,请不要忘记设置 API 版本高于 22。

在 setContentView 后面添加以下内容:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}

3
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);//  set status text dark

getWindow().setStatusBarColor(ContextCompat.getColor(MainActivity.this,R.color.colorPrimaryDark));// set status background white

对我来说它有效


这需要API级别23+。 - kanji

3

从 API 21+ 开始,这对我有效:

WindowInsetsControllerCompat windowInsetsController =
                WindowCompat.getInsetsController(getWindow(), getWindow().getDecorView());
windowInsetsController.setAppearanceLightStatusBars(true);

我使用

Theme.AppCompat.DayNight

这段代码适用于白天和夜晚模式:

getWindow().setStatusBarColor(getWindow().getNavigationBarColor());
WindowInsetsControllerCompat windowInsetsController =
                WindowCompat.getInsetsController(getWindow(), getWindow().getDecorView());
if ((getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_NO)
            windowInsetsController.setAppearanceLightStatusBars(true);
        else
            windowInsetsController.setAppearanceLightStatusBars(false);

正确答案在这里。 - Anorov Hasan

2

如果不是闪屏页面,请尝试此方法

getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
getActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getActivity().getWindow().setNavigationBarColor(ContextCompat.getColor(context, R.color.white));
getActivity().getWindow().setStatusBarColor(ContextCompat.getColor(context, R.color.white));

2
尝试这个。
在你的活动的 onCreate() 方法中,粘贴以下代码。"最初的回答"
try {
        if (android.os.Build.VERSION.SDK_INT >= 21) {
                Window window = getWindow();
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(ContextCompat.getColor(this, R.color.color_red));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

注意:color_red - 是状态栏颜色。 最初的回答:color_red代表状态栏的颜色。

4
问题是关于状态栏文字颜色,而不是状态栏背景。 - kanji

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接