最佳答案
您可以通过在 styles.xml< 中设置您用于应用的样式的 android:statusBarColor
或 android:colorPrimaryDark
属性来更改它/em>。
(android:statusBarColor
默认继承android:colorPrimaryDark
的值)
例如(因为我们在这里使用 AppCompat 主题,所以省略了 android
命名空间):
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimaryDark">@color/your_custom_color</item>
</style>
在 API 级别 21+ 上,您还可以使用 Window.setStatusBarColor()
代码中的方法。
来自 docs :
For this to take effect, the window must be drawing the system bar backgrounds with
WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS
andWindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
must not be set. If color is not opaque, consider settingView.SYSTEM_UI_FLAG_LAYOUT_STABLE
andView.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
.
要设置这些标志,您可以执行以下操作:
// getWindow() is a method of Activity
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
关于android - 如何更改Android中状态栏的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39341818/