如何将系统导航栏的背景颜色设置为Material 3中新的底部导航栏之一?(安卓)

Suj*_*mar 8 java android themes colors material-design

我尝试使用这个:

<item name="android:navigationBarColor">?attr/colorSurface</item>
Run Code Online (Sandbox Code Playgroud)

但它没有给我想要的结果..就像文档中显示的那样: 在此输入图像描述 在此输入图像描述

我最近切换到材质 3。

如果您想要应用程序的完整代码:https://github.com/Sujal1245/WALLisWALL-Wallpaper-App

小智 8

1.5.0 版(特别是 1.5.0-alpha03)及更高版本的 Material Components 库中有一个新类可以解决这个问题,即SurfaceColors类。

要使用它,请针对所需颜色的枚举getColor中的常量值之一调用该方法。SurfaceColors这是一个例子:

getWindow().setNavigationBarColor(SurfaceColors.SURFACE_2.getColor(this));
Run Code Online (Sandbox Code Playgroud)

SURFACE_2是 所使用的内容(或其等效内容)BottomNavigationView。无论有没有动态着色(包括夜间模式),它都可以工作。我还观察到 Google 文件应用程序中使用了此解决方案。

这是 Android 11 的屏幕截图

这是 Android 12.1 上动态着色的屏幕截图

如果您还对 Google 使用的确切阴影感到好奇BottomNavigationView,它是一个 5dp 高的渐变可绘制图,从 #00000000 到 #33333333。


小智 5

除了尼克的回答。如果您使用动态着色。设置动态着色后调用该方法。否则,您将始终获得相同的颜色而没有动态效果。

Kotlin 代码

DynamicColors.applyIfAvailable(this) // After this
window.navigationBarColor = SurfaceColors.SURFACE_2.getColor(this)
Run Code Online (Sandbox Code Playgroud)

谢谢。