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

138

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

这里输入图片描述

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

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

16个回答

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

    }
}

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

对于未来想要在 Android 中使用 Java 编程,以在片段中以编程方式将状态栏颜色从白色更改为主要暗色,并在离开片段时返回到最小 API 21<23的任何人

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

覆盖文本颜色? - Ayia

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

0
如果你想要改变状态栏中图标和文字的颜色,使其变为暗色以便更加清晰可见,你可以在Fragment的OnViewCreated方法中进行操作。请记住,这个操作必须在OnViewCreated方法中完成。
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        activity?.window?.let {
            WindowCompat.setDecorFitsSystemWindows(it, true)
            WindowInsetsControllerCompat(it, view).isAppearanceLightStatusBars = true
        }
    } else { // deprecated method for older versions
        var flags: Int = view.systemUiVisibility
        flags = flags or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        view.systemUiVisibility = flags
    }

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