本文介绍了底部导航视图-渐变图标色调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在BottomNavigationView中更改了图标色调,并在按下图标时使用了选择器和渐变颜色作为色调,但改为将图标色调更改为渐变Android使我的所有图标变为紫色.我的问题是是否有可能将BottomNavigationView中的图标色调更改为渐变颜色?

I have changed icon tint in BottomNavigationView using selector and gradient color as tint when icon is pressed, but instead changing icon tint to gradient Android made all my icon purple.My question is if there is any possibility to change tint of icons in BottomNavigationView to be gradient color?

BottomNavigationCode:

BottomNavigationCode:

<com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_bottom_main"
        android:layout_width="match_parent"
        app:itemIconTint="@color/bottom_menu_background"
        android:layout_height="wrap_content"
        app:menu="@menu/bottom_navigation_main"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

bottom_menu_background:

bottom_menu_background:

 <?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:color="@color/button_gradient_blue" android:state_checked="true" />
     <item android:color="@color/baseGray"/>
 </selector>

button_gradient_blue:

button_gradient_blue:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" >
        <shape android:shape="rectangle"  >
            <corners android:radius="@dimen/base_button_radius" />
            <gradient android:angle="0" android:startColor="@color/light_blue_gradient_start"
            android:endColor="@color/light_blue_gradient_end"  />
        </shape>
    </item>
    <item android:state_focused="true">
        <shape android:shape="rectangle"  >
            <corners android:radius="@dimen/base_button_radius" />
            <solid android:color="@color/button_pressed_color"/>
        </shape>
    </item>
    <item >
        <shape android:shape="rectangle"  >
            <corners android:radius="@dimen/base_button_radius" />
            <gradient android:angle="0" android:startColor="@color/light_blue_gradient_start"
            android:endColor="@color/light_blue_gradient_end" />
        </shape>
    </item>
</selector>

推荐答案

好吧,我设法通过使用两个不同的可绘制对象来做到这一点,一个带有渐变色,另一个在底部导航菜单中未选中.

Ok I managed to do this by using two different drawables, one with gradient tint and one for unselected in Bottom Navigation Menu.

我创建了自定义视图,该视图扩展了BottomNavigationView的形式,并且创建了用于创建菜单的自定义方法:

I have created custom view that extends form BottomNavigationView, and I have created custom method for creating menu:

fun initMenu(){
    itemIconTintList = null
    var firstStart = true
    setOnNavigationItemSelectedListener {
        val index = it.itemId
        firstStart = false
        menu.forEach {
            if (it.isChecked && !firstStart) {
                if (it.itemId == index)
                    return
                it.isChecked = false
                it.icon = ContextCompat
                    .getDrawable(context, UNSELECTED_MAP[it.itemId]!!)
        }

    }
    it.icon = ContextCompat.getDrawable(context, SELECTED_MAP[it.itemId]!!)

}

其中UNSELECTED_MAP和SELECTED MAP是一个HashMap,其项目菜单ID为键",可绘制资源为值"

Where UNSELECTED_MAP and SELECTED MAP are a HashMaps with item menu id as "key" and drawable resource as "value"

这篇关于底部导航视图-渐变图标色调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 21:39