Fer*_*osa 5 android bottomnavigationview android-bottom-nav-view
我需要在我的 android 应用程序中实现底部导航视图。中间的图标需要是图像,即公司徽标。但是当我运行该应用程序时,它只出现一个灰色填充的圆形图标。上面的图片显示了我想要的和我得到的。
我已经在这个网站上尝试过其他问题,但是每个答案都告诉我们使用可绘制对象更改 XML 中的 iconTintList,但中心图标是一个具有多种颜色的矢量。
当我尝试将 null 设置为 setIconTintList 方法时,适用于中间图标,但其他图标也会更改为原始颜色。
//This doesn't work to other icons, only for the middle one
mBottomNav.setItemIconTintList(null);
Run Code Online (Sandbox Code Playgroud)
我还尝试获取菜单并仅为中间的设置图标色调列表,就像上面的代码一样,但也不起作用。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mBottomNav.getMenu().findItem(R.id.nav_buy).setIconTintList(null);
}
Run Code Online (Sandbox Code Playgroud)
这是 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" />
Run Code Online (Sandbox Code Playgroud)
这是java实现:
mBottomNav = findViewById(R.id.bottomNavigationView);
mBottomNav.setOnNavigationItemSelectedListener(this);
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助!
Mur*_*KSU 20
我认为没有捷径可走。先用这个:
mBottomNav.setItemIconTintList(null);
Run Code Online (Sandbox Code Playgroud)
然后自己做设计。不要忘记将单击和未单击的按钮分开。
示例主页按钮 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>
Run Code Online (Sandbox Code Playgroud)
并将它们添加到视图中: 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>
Run Code Online (Sandbox Code Playgroud)
最后,将视图链接到底部导航视图
<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>
Run Code Online (Sandbox Code Playgroud)