如何在Android中更改状态栏的颜色? [英] How to change the color of the status bar in Android?
问题描述
我想在Android Studio中更改突出显示的栏的颜色:
I would like to change the color of highlighted bar in Android Studio:
我该怎么办?
推荐答案
您可以通过设置您所设置样式的 android:statusBarColor
或 android:colorPrimaryDark
属性来对其进行更改在 styles.xml 中用于您的应用.
You can change it by setting the android:statusBarColor
or android:colorPrimaryDark
attribute of the style you're using for your app in styles.xml.
(默认情况下, android:statusBarColor
继承 android:colorPrimaryDark
的值)
(android:statusBarColor
inherits the value of android:colorPrimaryDark
by default)
例如(由于我们在这里使用AppCompat主题,因此省略了 android
命名空间):
For example (since we're using an AppCompat theme here, the android
namespace is omitted):
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimaryDark">@color/your_custom_color</item>
</style>
在API级别21 + 上,您还可以使用 Window.setStatusBarColor()
方法中的代码.
On API level 21+ you can also use the Window.setStatusBarColor()
method from code.
从其文档:
要使其生效,该窗口必须绘制系统栏与背景
WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS
和不得设置WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
.如果颜色不是不透明,请考虑设置View.SYSTEM_UI_FLAG_LAYOUT_STABLE
和View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
.
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
.
要设置这些标志,您可以执行以下操作:
To set these flags you could do something like this:
// getWindow() is a method of Activity
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
这篇关于如何在Android中更改状态栏的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!