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

问题描述 投票:0回答:15

我正在尝试重现 Google 日历应用程序的行为:

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

有什么办法可以改变状态栏文字颜色吗?

android android-styles android-statusbar
15个回答
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>

38
投票

您可以像这样以编程方式完成此操作answer

只需添加这个

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

33
投票

兼容版本适用于 API 23+

这是:

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

您可以直接从

window
获得
Activity

我喜欢将其添加到

Window
扩展方法中:

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

为此您需要

androidx.core


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

16
投票

根据 @Jon 的回答,我会在新的 api 上对其进行一些更新。在具有主题和夜间主题(深色模式)的新 api 上,我将通过添加 v23/styles.xml 并在那里设置状态栏背景和文本颜色来实现:

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

在晚上/styles.xml:

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

默认的 styles.xml 不会包含任何此类代码,或者仅包含此代码,但请记住不要将其设置为浅色:

<item name="android:statusBarColor">?attr/colorPrimaryVariant</item>

通过这种方式,我们可以为状态栏设置浅色背景(和文本颜色),但仅适用于 api 23+ 的设备。在设备上 <23 background will not be changed, as I think this is something that we dont want knowing that the text color will stay white. The dark theme was added on API 29, so we don't have to be afraid of dark theme on api 21 ;)

然而,这样做的缺点是我们要添加另一个需要记住管理的文件。


4
投票

如前所述,SYSTEM_UI_FLAG_LIGHT_STATUS_BAR 在我的情况下完成工作,不要忘记设置为高于 API 22。

将其添加到 setContentView 之后的 oncreate 中:

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

它对我有用


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

2
投票

尝试一次。

在您的 Activity

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 - 是状态栏颜色。


2
投票

在您的活动

onCreate()
方法中,将以下代码粘贴到
setContentView(R.layout.activity_generic_main);

之后

下面是示例代码。

public class GenericMain extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_generic_main);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

    }
}

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

1
投票

要获得白色状态栏和黑色文本颜色,请执行以下操作(Kotlin):

在主要活动的

onCreate
功能中添加此

val window: Window = window
WindowInsetsControllerCompat(window,window.decorView).isAppearanceLightStatusBars = true

resoursces/styles.xml
添加这个

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:statusBarColor">#ffffff</item> <!-- this line sets the status bar color (in my case #ffffff is white) -->
<!-- the following lines are not required -->
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimary</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

这也适用于 API 级别 21。


0
投票

对于未来希望在片段中以编程方式将状态栏颜色从白色更改为最小 api 21 时保留原色的人< 23 in android using Java

private void updateStatusBar(boolean isEnter){

    Window window = requireActivity().getWindow();

    int color = ContextCompat.getColor(requireActivity(),R.color.colorAlertDialog);

    if(isEnter) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        else
            clearDecorFlags(window);
    }
    else {
        color = ContextCompat.getColor(requireActivity(),R.color.colorPrimaryDark);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
            window.getDecorView().setSystemUiVisibility(window.getDecorView().getSystemUiVisibility() & ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        else
            clearDecorFlags(window);
    }
    window.setStatusBarColor(color);
}

private void clearDecorFlags(Window window){
    window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
}

0
投票

所以在 kotlin 中有点不同

//for Dark status bar icon with white background

    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR)
            getWindow().setStatusBarColor(ContextCompat.getColor(this,R.color.white))    
    
             
    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv())
            getWindow().setStatusBarColor(ContextCompat.getColor(this,R.color.black))

 // for dark background and light theme colours of icon.

0
投票

撰写:

我在将主题从深色更改为浅色时遇到问题,反之亦然。 这些代码行对我有用:

val view = LocalView.current
val window = (view.context as Activity).window
window.statusBarColor = rememberedColors.surface.toArgb() //Some Color
WindowCompat.getInsetsController(window, view).isAppearanceLightStatusBars = isSystemInDarkTheme().not()
© www.soinside.com 2019 - 2024. All rights reserved.