如何从底部导航视图更改特定的图标图像 [英] How to change a specific icon image from Bottom Navigation View
问题描述
我需要在我的android应用中实现底部导航视图.中间的图标必须是图片,即公司徽标.但是,当我运行该应用程序时,它只会显示一个灰色填充的圆形图标.上面的图片显示了我想要的东西和得到的东西.
I need to implement a Bottom Navigation View in my android app. The middle icon needs to be an image, the company logo. But when I run the app it appears only a grey filled rounded icon. The images above show what I want and what I'm getting.
我想要什么:
我得到的是:
我已经在该网站上尝试了其他问题,但是每个答案都告诉您使用可绘制的内容从XML更改iconTintList,但是中心图标是具有多种颜色的矢量.
I already tried others questions in this website, but every answer tells to change the iconTintList from XML with a drawable, but the center icon is a vector with more than one color.
当我尝试将setIconTintList方法设置为null时,它适用于中间图标,但其他图标也更改为原始颜色.
When I tried to set null to setIconTintList method, works for the middle icon but the others icons change to original color too.
//This doesn't work to other icons, only for the middle one
mBottomNav.setItemIconTintList(null);
我也试图获取菜单,并只为中间菜单设置图标色调列表,就像上面的代码一样,但是也无法正常工作.
I also tried to get the menu and set the icon tint list only for the middle one, like the code above, but doesn't work too.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mBottomNav.getMenu().findItem(R.id.nav_buy).setIconTintList(null);
}
这是XML实现:
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@color/kmv_background"
app:itemIconTint="@drawable/bottom_nav_item_color"
app:itemTextColor="@drawable/bottom_nav_item_color"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_navigation" />
这是java实现:
mBottomNav = findViewById(R.id.bottomNavigationView);
mBottomNav.setOnNavigationItemSelectedListener(this);
感谢您的帮助!
推荐答案
我认为没有捷径可走.首先使用此:
I don't think there's a short way. Use this first:
mBottomNav.setItemIconTintList(null);
然后自己进行设计.别忘了按一下按钮而不是按一下按钮.
Then do the designs yourself. Don't forget to separate the buttons as clicked and not clicked.
主页按钮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
And add them to the view: Example 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>
最后,将视图链接到bottomnavigationview
And finally, Link view to bottomnavigationview
<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>
这篇关于如何从底部导航视图更改特定的图标图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!