android - 在 Fragment 更改时更改状态栏颜色 [Android Lollipop]

标签 android android-layout android-fragments android-5.0-lollipop

我想知道如何根据当前 Activity fragment (在 5.0 Lollipop 上)更改 StatusBar 颜色。目前,我根据所在的 Fragment 更改 ActionBar 颜色,但现在我希望 StatusBar 颜色也发生变化 - 为了在 Lollipop 设备上看起来不错。

我已经尝试使用 setStyle 以编程方式根据 fragment 更改主题,但它似乎并没有更改状态栏颜色。

如有任何想法,我们将不胜感激!

最佳答案

您是否尝试过使用 Window.setStatusBarColor 更改状态栏的颜色?例如,您可以执行以下操作以编程方式将状态栏颜色更改为红色。

getWindow().setStatusBarColor(Color.RED);

可以找到setStatusBarColor 的文档here .您还可以阅读有关如何 Customize the Status Bar 的文档.

注意:此方法只适用于Lollipop及以上版本。

如果您看到意外的颜色,请确保您传递的整数值是颜色整数,而不是资源 ID。

getWindow().setStatusBarColor(getResources().getColor(R.color.custom_color)); // RIGHT
getWindow().setStatusBarColor(R.color.custom_color); // WRONG

关于android - 在 Fragment 更改时更改状态栏颜色 [Android Lollipop],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26743039/

相关文章:

java - 方向不变(Actionbar 和 ICS)

android - 无法触摸 onPostExecute 的 AsyncTask 中的 View

android - 改变声音/点击软键盘 - android

Android:ImageView、LinearLayout 和TextView 高度返回负值,0 和0,为什么?

android - 动态更改 TextView 的样式和文本

android - 格式化 TextView

Android fragment 和动画

sax - 如何为 SAX 解析器设置线程和处理程序?

android - 从android中的Fragment访问工具栏textview

android - fragment 保存 View 状态