我已在项目中的Design Support Library 25中实现了Bottom Navigation View.我在视图中有5个图标.每当选择一个图标时,它就会有一些动画.但是当3个或更少的图标没有显示任何动画时.我想删除该动画,只需要对图标进行一些颜色更改.我怎样才能做到这一点?做了足够的谷歌搜索,但找不到解决方案.请帮忙.谢谢.
得到了这个帖子的答案.
删除动画或移位模式.
执行BottomNavigationView
条件:当有超过3项时,则使用shift模式.
创建助手类
import android.support.design.internal.BottomNavigationItemView; import android.support.design.internal.BottomNavigationMenuView; import android.support.design.widget.BottomNavigationView; import android.util.Log; import java.lang.reflect.Field; public class BottomNavigationViewHelper { 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) { Log.e("BNVHelper", "Unable to get shift mode field", e); } catch (IllegalAccessException e) { Log.e("BNVHelper", "Unable to change value of shift mode", e); } } }
用法
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation_bar); BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
我尝试了一下,效果很好
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
或这段代码mainactivity.xml
app:labelVisibilityMode="unlabeled"
BottomNavigationViewEx是标准BottomNavigationView的良好扩展.enableShiftingMode(false)为您完成工作.