Android利用系统原生BottomNavigationView实现底部导航

   <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="#2488e2"
  app:itemIconTint="@color/tab_icon" app:itemTextColor="@color/tab_icon" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:menu="@menu/navigation" />
itemIconTint,
itemTextColor:分别代表选中和非选中状态下按钮图标和文字的颜色

在color下新建tab_icon.xml,
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:color="@color/colorNormal" android:state_checked="false" />
    <item android:color="@color/colorSelected" android:state_checked="true"/>
</selector>

menu:在menu下建一个xxmenu.xml ,每一个item代表一个底部标签

 <item
        android:id="@+id/navigation_1"
        android:icon="@drawable/icon"
        android:title="@string/tab_title" />

 

当底部item过多的时候点击会有一个偏移的现象????

解决方法:定义一个工具类
public class BottomNavigationViewHelper {
    @SuppressLint("RestrictedApi")
    public static void disableShiftMode(BottomNavigationView view) {
        BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
        try {
            Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode");
            shiftingMode.setAccessible(true);
            shiftingMode.setBoolean(menuView, false);
            shiftingMode.setAccessible(false);
            for (int i = 0; i < menuView.getChildCount(); i++) {
                BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
                //noinspection RestrictedApi
                item.setShiftingMode(false);
                // set once again checked value, so view will be updated
                //noinspection RestrictedApi
                item.setChecked(item.getItemData().isChecked());
            }
        } catch (NoSuchFieldException e) {

        } catch (IllegalAccessException e) {

        }
    }

}

 



Android利用系统原生BottomNavigationView实现底部导航

上一篇:利用 html js判断 客户端是否安装了某个app 安装了就打开 否则跳转到gp


下一篇:[转帖]UEFI和BIOS