简体   繁体   English

Android - 更改每个选项卡底部导航的图标和标题的颜色

[英]Android - Change color of icon and title of each tab bottom navigation

I'm trying to make a bottom navigation a bit tricky.我试图让底部导航有点棘手。 Indeed I want this kind of bottom navigation:确实我想要这种底部导航: bottom_nav_1 bottom_nav_2 Each tab has a different color when it is selected.每个选项卡在被选中时都有不同的颜色。 For example measure will be in red when selected (icon and title) and profile will be green when selected...例如,选择时度量将显示为红色(图标和标题),选择时配置文件将显示为绿色...
So I tried to use a selector per item (in my menu)所以我尝试对每个项目使用一个选择器(在我的菜单中)
But the color is not applied.但是没有应用颜色。 The icon change successfully (I tried to put a completely different icon when an item is selected) but not the color of the title of the tab.图标更改成功(我尝试在选择项目时放置一个完全不同的图标)但不是选项卡标题的颜色。
I tried to remove 2 properties from my bottom navigation:我试图从底部导航中删除 2 个属性:

app:itemTextColor="@color/black"
app:itemIconTint="@color/black"

But it's getting worse because the color of my theme app (primary) is applied when a tab is selected.但它变得更糟,因为我的主题应用程序(主要)的颜色是在选择选项卡时应用的。

My bottom nav:我的底部导航:

<com.google.android.material.bottomnavigation.BottomNavigationView
                android:id="@+id/bottom_navigation"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/white"
                app:menu="@menu/main_bottom_navigation"
                style="@style/BottomNavigationView"
                app:labelVisibilityMode="labeled"
                android:layout_alignParentBottom="true" />

One of my selector (logic applied to all items):我的选择器之一(适用于所有项目的逻辑):

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- Pressed state -->
    <item android:drawable="@drawable/bottom_bar_journal_on"
            android:color="@color/red_FF623E"
            android:state_checked="true"/>
    <!-- Default state -->
    <item android:drawable="@drawable/bottom_bar_journal_off"
            android:color="@color/background_yellow"
            android:state_checkable="false"/>

</selector>

And my menu (where I apply all of my selector):还有我的菜单(我在其中应用了所有选择器):

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
            android:id="@+id/action_journal"
            android:enabled="true"
            android:icon="@drawable/bottom_bar_journal"
            android:title="@string/main_menu_journal"
            app:showAsAction="withText" />

    <item
            android:id="@+id/action_measure"
            android:enabled="true"
            android:icon="@drawable/bottom_bar_measure_off"
            android:title="@string/main_menu_measure"
            app:showAsAction="withText" />

    <item
            android:id="@+id/action_add"
            android:enabled="false"
            android:title=""
            app:showAsAction="withText" />

    <item
            android:id="@+id/action_treatment"
            android:enabled="true"
            android:icon="@drawable/bottom_bar_treatment_off"
            android:title="@string/main_menu_treatment" />

    <item
            android:id="@+id/action_profile"
            android:enabled="true"
            android:icon="@drawable/bottom_bar_profile"
            android:title="@string/main_menu_profile"
            app:showAsAction="withText" />
</menu>

Bottom navigation bar overrides icon colors via app:itemIconTint, but removing that attribute from the XML just makes the nav bar use the app's default colors instead.底部导航栏通过 app:itemIconTint 覆盖图标 colors,但从 XML 中删除该属性只会使导航栏使用应用程序的默认 Z62848E3CE5804AA985513A7922FF872B。 To prevent the bar from applying color changes, and let your selectors work as intended, you have to set the icon tint list to null in code, like this:为了防止栏应用颜色更改,并让您的选择器按预期工作,您必须在代码中将图标色调列表设置为 null,如下所示:

bottom_navigation_bar.itemIconTintList = null

EDIT: I see you want to color the text as well, that's a bit trickier.编辑:我看到您也想为文本着色,这有点棘手。 Only solution I can come up with is to forget about drawable selectors, and just replace the item's icon tint list and text color every time the bottom nav bar selection changes.我能想出的唯一解决方案是忘记可绘制选择器,只需在每次底部导航栏选择更改时替换项目的图标色调列表和文本颜色。 In your navbar-hosting activity, define these two:在您的导航栏托管活动中,定义这两个:

private val bottomNavBarStateList = arrayOf(
    intArrayOf(android.R.attr.state_checked),
    intArrayOf(-android.R.attr.state_checked)
)

private fun updateBottomNavBarColor(currentSelectedColor: Int) {
    val colorList = intArrayOf(
        ContextCompat.getColor(this, currentSelectedColor),
        ContextCompat.getColor(this, R.color.text_tertiary)
    )
    val colorStateList = ColorStateList(bottomNavBarStateList, colorList)
    bottom_navigation_bar.itemIconTintList = colorStateList
    bottom_navigation_bar.itemTextColor = colorStateList
}

Then in your navbar's ItemSelectedListener, call updateBottomNavBarColor with the color you want:然后在导航栏的 ItemSelectedListener 中,使用您想要的颜色调用 updateBottomNavBarColor:

bottom_navigation_bar.setOnNavigationItemSelectedListener {
    when(it.itemId) {
        R.id.bottom_nav_action_treatment -> {
            updateBottomNavBarColor(R.color.treatment)
        }
        R.id.bottom_nav_action_profile -> {
            updateBottomNavBarColor(R.color.profile)
        }
        else -> {

        }
    }
}

And you would use it like this on your BottomNavigationView:你会在你的 BottomNavigationView 上像这样使用它:

<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
app:itemIconTint="@drawable/bottom_navigation_colors"
app:itemTextColor="@drawable/bottom_navigation_colors"
app:menu="@menu/bottom_navigation_menu" />

The app:itemIconTint and app:itemTextColor take a ColorStateList instead of a simple color. app:itemIconTintapp:itemTextColor采用 ColorStateList 而不是简单的颜色。 This means that you can write a selector for these colors that responds to the items' state changes.这意味着您可以为这些 colors 编写一个选择器,以响应项目的 state 更改。

For example, you could have a bottom_navigation_colors.xml ColorStateList that contains:例如,您可能有一个包含以下内容的bottom_navigation_colors.xml ColorStateList:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item
      android:state_checked="true"
      android:color="@color/colorPrimary" />
  <item
      android:state_checked="false"
      android:color="@color/grey" />
 </selector>

used Colored Material Style使用有色材料样式

style="@style/Widget.MaterialComponents.BottomNavigationView.Colored"

Also Check out this Link: Hands-on with Material Components另请查看此链接: 使用材料组件动手操作

Also change colour by programmatically like this:也可以像这样以编程方式更改颜色:

getMenuInflater().inflate(R.menu.menu_home, menu);
        Drawable drawable = menu.findItem(R.id.action_clear).getIcon();

        drawable = DrawableCompat.wrap(drawable);
        DrawableCompat.setTint(drawable, ContextCompat.getColor(this,R.color.textColorPrimary));
        menu.findItem(R.id.action_clear).setIcon(drawable);
        return true;

Or:或者:

bottomNavigationView.setOnNavigationItemSelectedListener(
        new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.action_favorites:
                        //need change color of favotites here.
                    case R.id.action_schedules:

                    case R.id.action_music:

                }
                return true;
            }
        });

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

相关问题 底部导航图标颜色变化 - Bottom Navigation Icon color change 如何更改android studio底部导航栏上选择的图标颜色 - How to change the icon color selected on bottom navigation bar in android studio 如何在Android中更改屏幕底部的软导航背景和图标颜色? - How to change the bottom on screen soft navigation background and icon color in android? 为什么我无法使用渐变颜色 android 更改图标颜色和文本底部导航 - Why i can't change icon color and text bottom navigation with gradient color android Android-底部导航菜单更改图标图像 - Android - Bottom Navigation Menu change icon image 在Android中更改导航抽屉标题图标图像 - To change navigation drawer title icon image in Android Android Studio - onClick 片段事务不改变底部导航菜单图标颜色 - Android Studio - onClick Fragment Transaction does not change Bottom Navigation Menu Icon Color 如果我以编程方式更改 Fragment,如何更改底部导航图标颜色 - How to change bottom navigation icon color if I change Fragment programmatically Android studio - 更改 NOT OF BOTTOM NAVIGATION VIEW 的颜色 - Android studio - change color of NOT OF BOTTOM NAVIGATION VIEW 更改Android本机底部导航颜色? - Change Android native bottom navigation color?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM