setstatusbarcolor 后的透明状态栏导航抽屉

transparent statusbar navigationdrawer after setstatusbarcolor

我正在尝试使用 github 上的模板开发带有导航抽屉的应用程序。

style.xml 我有:

<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">#ff0000</item>
    <item name="colorPrimaryDark">#0000ff</item>

并且导航抽屉中的状态栏正常。

当我点击按钮时,它运行这个命令: getWindow().setStatusBarColor(Color.GREEN);

现在导航抽屉中的状态栏颜色不再是半透明的

如何将状态栏颜色恢复为半透明?

区别在于xml定义的颜色colorPrimarycolorPrimaryDark并不是真正用来直接设置状态栏颜色

实际上状态栏一直是完全透明的,只有底层 View 是彩色的。这就是为什么它的左侧可以有另一种颜色而不是右侧(看看你的第二个截图)。如果您现在调用 getWindow().setStatusBarColor(..),您确实 直接给状态栏上色 并过度绘制两者的颜色 Views。所以它需要保持透明!

您真正想要做的是更改状态栏下视图的颜色,这是通过 ScrimInsetsFrameLayout class.
完成的 看看 this question and this class from the library you provided
在那里你应该找到所有必要的信息来改变你想要的区域的颜色。


如果您真的只想重设颜色:

getWindow().setStatusBarColor(Color.TRANSPARENT);