【问题标题】:How to change the color of a specific element BottomNavigationView using ColorStateList?如何使用 ColorStateList 更改特定元素 BottomNavigationView 的颜色?
【发布时间】:2017-10-19 09:06:09
【问题描述】:

尝试为默认的BottomNavigationView 两种颜色(为菜单项的两种状态)添加第三种条件和第三种颜色。为此,在 drawable 中创建了一个带有选择器的文件“bottom_nav_colors.xml”:

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

带有布局模板“activity_main.xml”的文件指出:

<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:background="?android:attr/windowBackground"
    app:menu="@menu/navigation_menu"
    app:itemIconTint="@drawable/bottom_nav_colors"
    app:itemTextColor="@drawable/bottom_nav_colors"/>

但是,我无法弄清楚“MainActivity.java”类(从“MvpAppCompatActivity”类继承)中的“BottomNavigationView”类的什么方法可以将我的菜单项激活为“已激活”(或任何其他来自列表:“加速”、“活跃”等)。

如果你做不到,那怎么做?

【问题讨论】:

    标签: android xml android-studio bottomnavigationview appcompatactivity


    【解决方案1】:

    唉,“BottomNavigationView”这个技巧是行不通的。但如果你使用here 中的他的继承人,这一切都很容易完成:

    if (ServiceApp.getCartsGoodsNumber() > 0) {
    
        bnve.getBottomNavigationItemView(i).setIconTintList(
            ContextCompat.getColorStateList(
                bnve.getContext(),
                color.bottom_select_nav_colors));
        addBadgeAt(bnve, i, ServiceApp.getCartsGoodsNumber());
    
    } else {
    
        bnve.getBottomNavigationItemView(i).setIconTintList(null);
    
        if (mNavigation_20Badge != null) mNavigation_20Badge.hide(true);
    }
    

    这里有一个“BottomNavigationViewExe”类的实例,方法addBadgeAt()如下所示:

    private void addBadgeAt(BottomNavigationViewEx bnve, 
                            final int position, final int number) {
        // add badge
        mNavigation_20Badge = new QBadgeView(bnve.getContext())
                .setBadgeNumber(number).setGravityOffset(12, 2, true)
                .bindTarget(bnve.getBottomNavigationItemView(position));
    }
    

    此方法可让您添加徽章图标:

    enter image description here

    【讨论】:

      猜你喜欢
      • 2012-06-25
      • 1970-01-01
      • 1970-01-01
      • 2021-03-04
      • 1970-01-01
      • 1970-01-01
      • 2011-08-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多