gpt4 book ai didi

android - 如何从底部导航 View 更改特定图标图像

转载 作者:行者123 更新时间:2023-12-02 06:39:35 27 4
gpt4 key购买 nike

我需要在我的 Android 应用程序中实现底部导航 View 。中间的图标需要是一个图像,即公司 Logo 。但是当我运行该应用程序时,它只显示一个灰色填充的圆形图标。上面的图片显示了我想要的和我得到的。

我想要什么: enter image description here

我得到了什么: enter image description here

我已经在这个网站上尝试过其他问题,但每个答案都告诉我们用可绘制的 XML 更改 iconTintList,但中心图标是一个具有多种颜色的矢量。

当我尝试将 null 设置为 setIconTintList 方法时,适用于中间图标,但其他图标也更改为原始颜色。

//This doesn't work to other icons, only for the middle one 
mBottomNav.setItemIconTintList(null);

我还尝试获取菜单并仅为中间的菜单设置图标色调列表,如上面的代码,但也不起作用。

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);

感谢您的帮助!

最佳答案

我认为没有捷径。首先使用这个:

   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>

并将它们添加到 View 中:示例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>

最后,将 View 链接到底部导航 View

<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 - 如何从底部导航 View 更改特定图标图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57861254/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com