You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android如何修改状态栏图标颜色?适配enableEdgeToEdge界面

问题与解决方案:动态调整状态栏图标颜色适配图片画廊背景

我使用enableEdgeToEdge()函数开发了一个新界面,该界面的状态栏下方展示图片画廊。根据API返回的图片差异,有时状态栏图标(例如时间)会因背景图片影响而无法清晰显示。我希望修改状态栏文字或图标颜色(至少改为白色),但不想采用自定义状态栏遮罩(会遮挡图片且文字仍为黑色)、修改应用主题或过于复杂的方式,请问是否有合适的实现方法?

解决方案(2024年9月9日)

在Kotlin Fragment中实现

通过系统API直接控制状态栏图标亮度,同时可按需设置状态栏背景色,无需修改主题或添加遮罩:

override fun onAttach(context: Context) {
    super.onAttach(context)
    if (context is SpotlightInterface) spotlightHelper = context
    mainActivity?.window?.let {
        it.statusBarColor =
            ContextCompat.getColor(context, R.color.color_content_inverse)
        it.navigationBarColor =
            ContextCompat.getColor(context, R.color.color_content_inverse)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            val controller = WindowCompat.getInsetsController(it, it.decorView)
            // true表示图标为黑色,false表示图标为白色
            controller.isAppearanceLightStatusBars = true
            controller.isAppearanceLightNavigationBars = true
        } else {
            it.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        }
    }
}

在Jetpack Compose中实现

利用LaunchedEffect监听状态变化,动态切换状态栏图标颜色与背景色:

LaunchedEffect(type) {
    if (type == DEFAULT) {
        window.statusBarColor = Color.TRANSPARENT
        // 设置为false,状态栏图标变为白色
        wic.isAppearanceLightStatusBars = false
    } else {
        window.statusBarColor = Color.WHITE
        // 设置为true,状态栏图标变为黑色
        wic.isAppearanceLightStatusBars = true
    }
}

内容的提问来源于stack exchange,提问作者Sergio

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 20:12:45