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

3
我已经尝试了一切来实现这个功能,但是失败了,“drawerLayout.setStatusBarBackground()”不起作用,“android:windowTranslucentStatus:true”也不起作用。
这里,第一张图片是我现在能做到的全部,第二张图片是期望的效果: 现在能做到的全部 期望的效果 非常感谢。

https://dev59.com/x2Eh5IYBdhLWcg3wpEtF - Pankaj Kumar
谢谢回复,但我想问的是导航抽屉状态栏(当您滑出时,最顶部的栏),而不是默认状态栏(尽管它们是相同的...但我想更改其中某些部分的颜色)。 - Sam Chen
你能够解决它吗?我知道的是这种行为只适用于 NavigationView,如果你使用 FrameLayout,你可以通过使用半透明状态栏和 android:windowDrawsSystemBarBackgrounds 来实现这个效果。不过你需要手动处理插图。 - Alpha
1个回答

6
问题在于NavigationView扩展了ScrimInsetsFrameLayout,该布局侦听窗口插图更改并向布局添加了暗线。ScrimInsetsFrameLayout相同具有可定义样式属性insetForeground,至少在com.google.android.material:material:1.1.0-alpha07中存在。
因此,如果您使用:
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:statusBarColor">@color/transparent</item>

你只需要将 insetForeground 设置为 @color/transparent 即可。
例如:
<com.google.android.material.navigation.NavigationView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    app:insetForeground="@color/transparent"/>

警告: 如果您设置了android:fitsSystemWindows="true",此方法将无效。

注意: 使用此方法意味着您必须手动处理插图,因为您正在使用半透明状态而不是适应系统窗口。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接