如https://www.google.co.in/design/spec/layout/struction.html#structural-system-bars中所示。
现在,我可以使用
更改导航栏的背景
"android:navigationBarColor"
但似乎无法将按钮颜色更改为深色。
任何人都知道如何去做。
PS:
在 AOSP 中研究负责导航按钮的类时,我可以找到
NavigationBarView.java、PhoneStatusBar.java、PhoneWindowManager.java、navigation_bar.xml.
我正在考虑获取导航栏按钮(如ic_sysbar_recent
)的可绘制参考并更改其色调。但这些都是私人的,我无法得到他们的参考。另外,我见过有人使用 xposed 库来做到这一点
L-NAVBAR,但我不想使用任何外部库。
<item name="android:windowLightNavigationBar">true</item>
您可以创建一个名为
values-v27 的文件夹,并放置带有浅色导航栏的主题版本(在 styles.xml 中)和上面的代码以获得深色导航栏按钮。
这样,使用 Android 8.0 或更低版本的用户将获得标准(黑色)导航栏,而使用 Android 8.1 或更高版本的用户将获得带有黑色按钮的白色导航栏。
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="android:windowBackground">@color/your_color</item>
</style>
但是如果您的目标用户 api 级别低于 27 或更高,您可以使用这些行
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="android:windowBackground">@color/your_color</item>
<item name="android:windowLightNavigationBar" tools:targetApi="27">true</item>
</style>
通过这样做,API级别27或更高的用户可以改变BottomNav图标的颜色,但是API级别低于27的用户无法享受这些功能
getWindow().setNavigationBarColor(getContext().getResources().getColor(R.color.white));
View view = getWindow().getDecorView();
view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
导航栏图标 - 白色
getWindow().setNavigationBarColor(getContext().getResources().getColor(R.color.black));
View view = getWindow().getDecorView();
view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
在片段中
getActivity().getWindow().setNavigationBarColor(getContext().getResources().getColor(R.color.white));
View view = getActivity().getWindow().getDecorView();
view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
导航栏图标 - 白色
getActivity().getWindow().setNavigationBarColor(getContext().getResources().getColor(R.color.black));
View view = getActivity().getWindow().getDecorView();
view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
Theme.kt
。使用动态配色方案时,需要设置
isNavigationBarContrastedEnforced == true
才能使导航图标正确对比。 (例如
dynamicDarkColorScheme(context)
)
val view = LocalView.current
val darkTheme = isSystemInDarkTheme()
val dynamicColor:Boolean // from function argument
if (!view.isInEditMode) {
SideEffect {
val window = (view.context as Activity).window
window.statusBarColor = colorScheme.surface.toArgb()
window.navigationBarColor = colorScheme.surface.toArgb()
// Ensures text in status bars contrasts
WindowCompat.getInsetsController(window, view).isAppearanceLightStatusBars = !darkTheme && dynamicColor
WindowCompat.getInsetsController(window, view).isAppearanceLightNavigationBars = !darkTheme && dynamicColor
// Ensures navigation bar icons contrast.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
window.isNavigationBarContrastEnforced = true
}
}
}
if(darkIcon){
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.getDecorView().getWindowInsetsController().setSystemBarsAppearance(APPEARANCE_LIGHT_STATUS_BARS, APPEARANCE_LIGHT_STATUS_BARS);
}
}else {
window.getDecorView().setSystemUiVisibility(0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.getDecorView().getWindowInsetsController().setSystemBarsAppearance(0, APPEARANCE_LIGHT_STATUS_BARS);
}
}
val navView: BottomNavigationView = findViewById(R.id.nav_view)
navView.itemIconTintList = null