简体   繁体   English

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

[英]Change navigation bar icon color on Android

I need to change the navigation bar on android. Just like the 'light' variant on the right in the image below我需要更改 android 上的导航栏。就像下图中右侧的“light”变体一样 as given in https://www.google.co.in/design/spec/layout/structure.html#structure-system-bars .https://www.google.co.in/design/spec/layout/structure.html#structure-system-bars中给出的。

Now, I can change the background of the navigation bar by using现在,我可以使用更改导航栏的背景

"android:navigationBarColor"

I get我得到

在此处输入图像描述

but there seems to be no way changing the button color to dark.但似乎无法将按钮颜色更改为深色。

Anyone has the idea how to do it.任何人都知道如何去做。

PS:附言:

While researching in AOSP for the classes responsible for NavigationButtons, I could find NavigationBarView.java , PhoneStatusBar.java , PhoneWindowManager.java , navigation_bar.xml .在 AOSP 中研究负责导航按钮的类时,我可以找到NavigationBarView.javaPhoneStatusBar.javaPhoneWindowManager.javanavigation_bar.xml

I was thinking of get reference of the drawable for the navigationbar buttons like ic_sysbar_recent and change their tint.我正在考虑为ic_sysbar_recent之类的导航栏按钮获取可绘制对象的引用并更改它们的色调。 But these are private and I cannot get their reference.但这些都是私人的,我无法获得他们的参考。

Also, I have seen people using xposed library to do it L-NAVBAR , but I don't want to use any external library.另外,我看到有人使用 xposed 库来做L-NAVBAR ,但我不想使用任何外部库。

If you are using API 27 (Android 8.1) or greater, you can achieve it with this in your theme: 如果您使用的是API 27(Android 8.1)或更高版本,则可以在主题中实现此目的:

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

You can create a folder called values-v27 and place a version of your theme (in styles.xml) with a light navigation bar and the above code to get dark navigation bar buttons. 您可以创建一个名为values-v27的文件夹,并使用灯光导航栏和上面的代码放置主题版本(在styles.xml中)以获取暗导航栏按钮。
This way, users with Android 8.0 or lower will get the standard (black) navigation bar while users with Android 8.1 or greater will get a white navbar with dark buttons. 这样,使用Android 8.0或更低版本的用户将获得标准(黑色)导航栏,而使用Android 8.1或更高版本的用户将获得带有暗按钮的白色导航栏。

Starting from Android O it gets really straightforward, as you could just: 从Android O开始,它非常简单,你可以:

View.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);

For this to take effect, the window must request FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS but not FLAG_TRANSLUCENT_NAVIGATION. 为使此操作生效,窗口必须请求FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS,但不能请求FLAG_TRANSLUCENT_NAVIGATION。

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

You can adjust windowLightNavigationBar = true/false and navigationBarColor = @color/yourColorId您可以调整windowLightNavigationBar = true/falsenavigationBarColor = @color/yourColorId

so there're 4 cases, I've made an experiment:所以有 4 种情况,我做了一个实验:

In short, you don't want windowLightNavigationBar= false while navigationBarColor=White简而言之,您不希望windowLightNavigationBar= falsenavigationBarColor=White

or要么

windowLightNavigationBar= true while navigationBarColor=Black (this will be wired on SOME devices) windowLightNavigationBar= truenavigationBarColor=Black (这将在某些设备上连接)

在此处输入图像描述

If your targeted user Api level is 27 or Higher just use this line in your AppTheme如果您的目标用户 Api 级别为 27 或更高,只需在您的 AppTheme 中使用此行

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">       
    <item name="android:windowBackground">@color/your_color</item>                
</style>

But if your targeted user api level is less 27 and higher you can you these line但是,如果您的目标用户 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>

By doing so, The user having API level 27 or higher get changed-color of BottomNav icon, but the user having API level less than 27, can't enjoy these feature通过这样做,具有 API 级别 27 或更高级别的用户会更改 BottomNav 图标的颜色,但是具有 API 级别低于 27 的用户无法享受这些功能

    // IF your want code in Activity :

    // if you want navigation-bar icon color drak.

        getWindow().setNavigationBarColor(getContext().getResources().getColor(R.color.white));
        View view = getWindow().getDecorView();
        view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);

    // if you want navigation-bar icon color white.

        getWindow().setNavigationBarColor(getContext().getResources().getColor(R.color.black));
        View view = getWindow().getDecorView();
        view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
    

    // IF your want code in Fragment :

    // if you want navigation-bar icon color drak.

        getActivity().getWindow().setNavigationBarColor(getContext().getResources().getColor(R.color.white));
        View view = getActivity().getWindow().getDecorView();
        view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);

    // if you want navigation-bar icon color white.

        getActivity().getWindow().setNavigationBarColor(getContext().getResources().getColor(R.color.black));
        View view = getActivity().getWindow().getDecorView();
        view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
    }

If you are looking for a simple java method.如果您正在寻找一种简单的 java 方法。 The following code works以下代码有效

public static void setStatusBarColor(Activity A , int color , int color2 , boolean darkIcon) {
   
      if (A != null && !A.isFinishing()) {

           Window window = A.getWindow();

           window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

           window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
           
           if(darkIcon){
                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
           }else {
                window.getDecorView().setSystemUiVisibility(0);
           }

           window.setStatusBarColor(ContextCompat.getColor(A, color));
           window.setNavigationBarColor(ContextCompat.getColor(A,color2));

      }
 }

Inside Activity内部活动

  @Override
protected void onDestroy() {
    super.onDestroy();
    C.setStatusBarColor(this ,R.color.bg , R.color.navcolor ,false);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    C.setStatusBarColor(this ,R.color.bg , R.color.bg,true);
    setContentView(R.layout.activity_teams_players);
}

From what I can tell, those Icons are part of com.android.systemui, which is why you need root + patch or injection (like Xposed) to be able to change them. 据我所知,这些图标是com.android.systemui的一部分,这就是为什么你需要root + patch或者注入(比如Xposed)才能改变它们。

Consider it from a different perspective. 从不同的角度考虑它。 One possibility might be setting the theme for that activity to light or dark, then programmatically setting the background color of the bar. 一种可能性可能是将该活动的主题设置为亮或暗,然后以编程方式设置条的背景颜色。 One of those themes should have the dark icons. 其中一个主题应该有黑色图标。

Another alternative could be to hide the bar altogether https://developer.android.com/training/system-ui/navigation.html 另一种选择可能是完全隐藏条形图https://developer.android.com/training/system-ui/navigation.html

and then create a second Toolbar on the bottom with your own buttons. 然后使用您自己的按钮在底部创建第二个工具栏。

Simply add This line you are ok while the app is running只需在应用程序运行时添加这一行就可以了

val navView: BottomNavigationView = findViewById(R.id.nav_view)
    navView.itemIconTintList = null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM