更改 Android 上的导航栏图标颜色

问题描述 投票:0回答:8
我需要更改 Android 上的导航栏。就像下图中右侧的“浅色”变体一样

https://www.google.co.in/design/spec/layout/struction.html#structural-system-bars中所示。
现在,我可以使用
更改导航栏的背景

"android:navigationBarColor"



我明白了

但似乎无法将按钮颜色更改为深色。

任何人都知道如何去做。

PS:

在 AOSP 中研究负责导航按钮的类时,我可以找到

NavigationBarView.javaPhoneStatusBar.javaPhoneWindowManager.javanavigation_bar.xml.

我正在考虑获取导航栏按钮(如

ic_sysbar_recent

)的可绘制参考并更改其色调。但这些都是私人的,我无法得到他们的参考。 

另外,我见过有人使用 xposed 库来做到这一点

L-NAVBAR,但我不想使用任何外部库。

android material-design
8个回答
70
投票
如果您使用 API 27 (Android 8.1) 或更高版本,您可以在主题中使用此功能来实现:

<item name="android:windowLightNavigationBar">true</item>

您可以创建一个名为

values-v27 的文件夹,并放置带有浅色导航栏的主题版本(在 styles.xml 中)和上面的代码以获得深色导航栏按钮。 这样,使用 Android 8.0 或更低版本的用户将获得标准(黑色)导航栏,而使用 Android 8.1 或更高版本的用户将获得带有黑色按钮的白色导航栏。


30
投票
从 Android O 开始,它变得非常简单,因为您可以:

View.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);

要使其生效,窗口必须请求 FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS 但不是 FLAG_TRANSLUCENT_NAVIGATION。

文档:

https://developer.android.com/reference/android/view/View.html#SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR


6
投票
可以调整

windowLightNavigationBar = true/false

navigationBarColor = @color/yourColorId

所以有4种情况,我做了一个实验:

简而言之,你不想要

windowLightNavigationBar= false

同时
navigationBarColor=White

windowLightNavigationBar= true

同时
navigationBarColor=Black
(这将在某些设备上连接)


3
投票
如果您的目标用户 Api 级别为 27 或更高,只需在您的 AppTheme 中使用此行

<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的用户无法享受这些功能


0
投票
活动中

导航栏图标 - 深色

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);
    

0
投票
将下面的代码块添加到

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 } } }
    

-1
投票
要获取深色和浅色图标,请使用以下代码

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); } }
    

-3
投票
只需在应用程序运行时添加此行就可以了

val navView: BottomNavigationView = findViewById(R.id.nav_view) navView.itemIconTintList = null
    
© www.soinside.com 2019 - 2024. All rights reserved.