androidx使用BottomNavigationView时取消位移动画

在Android项目中,使用androidx或'support:design:28.0.0'时,BottomNavigationView超过3个item会产生位移动画。本文介绍了如何通过反射禁用这一动画。关键在于设置labelVisibilityMode为1和isShifting为false,以实现无动画效果。需要注意支持库版本差异,不同版本可能需要不同的处理方式。
摘要由CSDN通过智能技术生成

  Android 项目在使用androidx 或者 "support:design:28.0.0"依赖时,取消BottomNavigationView大于3个item的位移动画

 最近新项目要使用底部导航BottomNavigationView控件,但是在底部item大于3个的时候就会有位移动画,我们需要禁用位移动画,(作为面向网络编程的程序员)我上网一搜索,复制粘贴过来:

    /**
     * 解决BottomNavigationView大于3个item时的位移
     * @param view
     */
    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);

                item.setShiftingMode(false);

                item.setChecked(item.getItemData().isChecked());
            }
        } catch (NoSuchFieldException e) {
            Log.e("BNVHelper", "Unable to get shift mode field", e);
        } catch (IllegalAccessException e) {
            Log.e("BNVHelper&#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值