标签:log 选择 cti black find 前言 tom coding 解决
注意这个里介绍的是AndroidX的com.google.android.material.bottomnavigation.BottomNavigationView
<com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/bottom_navigation_view" android:layout_width="0dp" android:layout_height="wrap_content" app:menu="@menu/p_home_bottom_menu" app:labelVisibilityMode="labeled" app:itemTextColor="@color/fontBlack1" app:itemTextAppearanceActive="@style/Active" app:itemTextAppearanceInactive="@style/Inactive" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent"> </com.google.android.material.bottomnavigation.BottomNavigationView>
<style name="Active" parent="@style/TextAppearance.AppCompat.Caption"> <item name="android:textSize">@dimen/font_size_17</item> </style> <style name="Inactive" parent="@style/TextAppearance.AppCompat.Caption"> <item name="android:textSize">@dimen/font_size_11</item> </style>
只是改变文字大小
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/home" android:title="首页" android:icon="@mipmap/ic_admission"/> <item android:id="@+id/notice" android:title="通知" android:icon="@mipmap/ic_head"/> <item android:id="@+id/circle" android:title="圈子" android:icon="@mipmap/ic_temp"/> <item android:id="@+id/my_info" android:title="我的" android:icon="@mipmap/ic_notice"/> </menu>
图标添加后你会发现图标会被Tint颜色覆盖变成灰色的图标,下面这两行代码解决这个问题
mBottomNavigationView = findViewById(R.id.bottom_navigation_view); mBottomNavigationView.setItemIconTintList(null);
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="false" android:drawable="@drawable/ic_home_page_normal"/> <item android:state_checked="true" android:drawable="@drawable/ic_home_page_selected"/> </selector>
在menu的item上调用
<item android:id="@+id/navigation_home" android:icon="@drawable/ic_home_black_24dp" android:title="@string/title_home" />
mBottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) { Log.e("ytzn", "onNavigationItemSelected: 选中"+menuItem.getItemId() ); return true; } }); mBottomNavigationView.setOnNavigationItemReselectedListener(new BottomNavigationView.OnNavigationItemReselectedListener() { @Override public void onNavigationItemReselected(@NonNull MenuItem menuItem) { Log.e("ytzn", "onNavigationItemSelected: 选中状态再次选中"+menuItem.getItemId() ); } });
setOnNavigationItemSelectedListener 是点击未选择的item后的回调,返回的boolean是决定是否启用选中效果或者放大效果
setOnNavigationItemReselectedListener 是如果已经是选中状态后,在点击一次后的回调
end
Android 开发BottomNavigationView学习
标签:log 选择 cti black find 前言 tom coding 解决
原文地址:https://www.cnblogs.com/guanxinjing/p/11027145.html