- 开始使用
compile 'com.android.support:design:25.3.1'
- 常用属性
android:background : 控件背景
app:itemBackground : 子菜单背景
app:itemIconTint : 图标颜色
app:itemTextColor : 文本颜色
app:menu : 菜单
- 图标颜色选择器
app:itemIconTint="@color/seletor_bottom_navi"
- 图标图片选择器
menu中icon 用一个图片seletor
- app:itemBackground
会替换掉原有点击水波纹效果
- app:menu
app:menu="@menu/menu_bottom_navigation_view"
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/item_1"
android:icon="@mipmap/ic_launcher"
android:title="消息" />
<item
android:id="@+id/item_2"
android:icon="@mipmap/ic_launcher_round"
android:title="联系人"
/>
<item
android:id="@+id/item_3"
android:icon="@mipmap/ic_launcher"
android:title="动态" />
<item
android:id="@+id/item_4"
android:icon="@mipmap/ic_launcher_round"
android:title="我的" />
</menu>
心得体会
- 常规配置
private void initBnve() {
idBnvs.enableMenuViewShiftMode(false);//取消菜单view的展开收缩模式
idBnvs.enableItemViewShiftingMode(false);//取消菜单view的Item 展开收缩模式 (文字 显示隐藏)
/* idBnvs.setItemIconTintList(null);//不是使用主题中的颜色,恢复图标本身颜色*/
idBnvs.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@android.support.annotation.NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.id_menu_item_1:
break;
case R.id.id_menu_item_2:
break;
case R.id.id_menu_item_3:
break;
case R.id.id_menu_item_4:
break;
}
// return false;
return true;//返回 true 使点击有效
}
});
idBnvs.setChecked(1);//选择
idBnvs.setChecked(2);
/* idBnvs.setClickedItem(R.id.id_menu_item_2);//选择
idBnvs.setClickedItem(R.id.id_menu_item_3);*/
}
- BottomNavigationViewSupport.java
public class BottomNavigationViewSupport extends BottomNavigationView {
private BottomNavigationMenuView mMenuView;
private BottomNavigationItemView[] mButtons;
public BottomNavigationViewSupport(Context context) {
this(context, null);
}
public BottomNavigationViewSupport(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public BottomNavigationViewSupport(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//
this.enableMenuViewShiftMode(false);
this.enableItemViewShiftingMode(false);
}
/**
* 根据索引选中对应Item
* 推荐
*
* @param index
*/
public void setChecked(int index) {
if (index >= 0) {
getMenu().getItem(index).setChecked(true);
}
}
/**
* 根据menu item id选中对应Item
* 和 setChecked 功能一致
*
* @param menu_item_id
*/
public void setClickedItem(int menu_item_id) {
View view = findViewById(menu_item_id);
if (view != null) {
view.performClick();
}
}
public void enableMenuViewShiftMode(boolean enable) {
if (mMenuView == null) {
mMenuView = ReflectTool.getFieldValueFromSuperClass(BottomNavigationView.class, this, "mMenuView");
}
ReflectTool.setFieldValue(mMenuView, "mShiftingMode", enable);
// mMenuView.updateMenuView();
}
public void enableItemViewShiftingMode(boolean enable) {
if (mMenuView == null) {
mMenuView = ReflectTool.getFieldValueFromSuperClass(BottomNavigationView.class, this, "mMenuView");
}
if (mMenuView == null) {
return;
}
if (mButtons == null) {
mButtons = ReflectTool.getFieldValue(mMenuView, "mButtons");
}
for (BottomNavigationItemView button : mButtons) {
ReflectTool.setFieldValue(button, "mShiftingMode", enable);
}
mMenuView.updateMenuView();
}
}
- ReflectTool.java
/**
* 反射获得对象的值
*
* @param
* @param fieldName
* @return
*/
public static <T, E> T getFieldValue(E eObj, String fieldName) {
try {
Field field = eObj.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
return (T) field.get(eObj);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
public static <E> void setFieldValue(E eObj, String fieldName, Object value) {
try {
Field field = eObj.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
field.set(eObj, value);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
/**
* 反射获得对象的值
* 常用于 子类继承父类调用
* @param
* @param fieldName
* @return
*/
public static <T> T getFieldValueFromSuperClass(Class tClass, Object obj, String fieldName) {
try {
Field field = tClass.getDeclaredField(fieldName);
field.setAccessible(true);
return (T) field.get(obj);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
public static void setFieldValueForSuperClass(Class tClass, Object obj, String fieldName, Object value) {
try {
Field field = tClass.getDeclaredField(fieldName);
field.setAccessible(true);
field.set(obj, value);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
-
参考文档
-
Android BottomNavigationView,底部导航栏的简单实现 http://www.tuicool.com/articles/jyEBFfB
-
BottomNavigationView从入门到强行改造,取消位移动画?和ViewPager绑定?添加Badge? http://blog.csdn.net/qq_35064774/article/details/54177702