android - 如何更改Android中状态栏的颜色?

标签 android

我想在 Android Studio 中更改高亮条的颜色:

enter image description here

我该怎么做?

最佳答案

您可以通过在 styles.xml< 中设置您用于应用的样式的 android:statusBarColorandroid: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 and WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS must not be set. If color is not opaque, consider setting View.SYSTEM_UI_FLAG_LAYOUT_STABLE and View.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/

相关文章:

android - android 上的 react-apollo 性能问题

android - 如何使用户无法返回,除非他们单击按钮

android - 如何将动态自定义 View 嵌入到 xml 布局中?

java - 日历开始日期早于结束日期的同一天差异

java - ListView 不会显示 - 空白屏幕

android - 带有滑动 TabLayout 的 ViewPager 中的 Listview - 高度问题

android - 未选中项目时,Listview.getCheckedItemPositions() 未返回正确结果

java - 如何再次从 firebase 数据库中请求数据?

java - 如何在Activity onCreate中更改ImageView源

android - 从android中的相机 Intent 获取uri