【问题标题】:BottomNavigationView - Android StudioBottomNavigationView - Android Studio
【发布时间】:2021-03-03 13:13:45
【问题描述】:

我使用图像作为图标,但我不希望它们在未选中时看起来是灰色的。无论它是否处于活动状态,它都应该与图片相同。我该怎么办?

com.google.android.material.bottomnavigation.BottomNavigationView

谢谢

【问题讨论】:

    标签: android android-layout bottomnavigationview android-developer-api android-bottomnavigationview


    【解决方案1】:

    尝试在您的 XML 文件的 BottomNavigationView 部分中使用它

    app:itemIconTint="null"
    

    或将其添加到您的 java 文件中定义您的 BottomNavigationView(使用 findViewById)

    //Replace BottomNavView with your nav bar id
    BottomNavView.setItemIconTintList(null);
    

    【讨论】:

    • 我试过这个但我得到这个错误 \res\layout\activity_home.xml:44: AAPT: error: 'null' is incompatible with attribute itemIconTint (attr) color [weak]。
    • 尝试将其添加到活动文件(java 文件)“BottomNavView.setItemIconTintList(null);”之后使用 findViewById 定义您的 BottomNavView
    • @Nina 您必须使用@null 而不是null,但这样您将删除默认选择器。
    • 嗨,很抱歉,我不知道该怎么办。您可以尝试为每个单独的项目设置色调。像这样的东西:Menu BVMenu = bottomNavigationView.getMenu(); BVMenu.setItemIconTintList(null); BVMenu.findItem(R.id.Your_menu_item)name).setIcon(yout_drawable);
    • ^^这将在你初始化视图组件的活动的java文件中,可以在OnCreate上,记得先使用findViewById定义你的bottomNavigationView
    【解决方案2】:

    实现它还有很长的路要走

    mBottomNav.setItemIconTintList(null);
    

    然后自己做设计。不要忘记将按钮区分为单击和未单击。

    示例按钮 XML

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <!--Clicked-->
        <item android:drawable="@drawable/homeclicked" android:state_checked="true" />
        <!--Not Clicked-->
        <item android:drawable="@drawable/homenotclicked" android:state_checked="false" />
    </selector>
    

    并将它们添加到视图中:示例 bottom_navigation.xml

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item
            android:id="@+id/homebuttons"
            android:icon="@drawable/homebuttonxml />
    
     <!--Other Buttons...-->
    
    </menu>
    

    最后,将视图链接到底部导航视图

    <com.google.android.material.bottomnavigation.BottomNavigationView    
         android:id="@+id/bottomNavigationView"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         app:labelVisibilityMode="unlabeled"
         app:elevation="0dp"
         app:menu="@menu/bottom_navigation">  
       
    </com.google.android.material.bottomnavigation.BottomNavigationView>
    

    【讨论】:

    • 我是 android 开发新手,我不知道把这些代码放在哪里:(。你能帮帮我吗
    【解决方案3】:

    未选中项使用的默认颜色基于colorOnSurface颜色。

    只需使用:

    <com.google.android.material.bottomnavigation.BottomNavigationView
         android:theme="@style/BottomNavigationViewThemeOverlay"
         ....  />
    

    与:

    <style name="BottomNavigationViewThemeOverlay">
        <item name="colorOnSurface">@color/...</item>
    </style>
    

    否则你可以定义你的自定义选择器:

      <com.google.android.material.bottomnavigation.BottomNavigationView
               app:itemIconTint="@color/bottomnavicontint"
               ../>
    

    与:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:alpha="1.0" android:color="?attr/colorPrimary" android:state_checked="true"/>
        <item android:alpha="0.6" android:color="@color/..."/>. <!-- not selected -->
    </selector>
    

    【讨论】:

    • @AshutoshYadav 您使用的是哪个版本?刚刚检查了这两种方法,它们适用于 1.2.1 和 1.3.0
    • 。我必须在那里设置什么颜色?
    • @AshutoshYadav 您想要为未选择的项目实现的颜色。
    • 我不想为未选择的项目使用任何颜色,实际上我使用的是 png 他们有自己的颜色,我只是希望在我们选择而不是选择它时它不会被颜色重叠跨度>
    • @AshutoshYadav 在这种情况下你必须使用navView.itemIconTintList = null
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多