Android 单击图标时,如何在底部导航视图中禁用动画?
我正在为我的应用程序使用默认的底部视图导航栏,它有4个按钮,它们有一个可怕的移动动画,而且compat库中似乎没有一个方法。禁用它。请帮忙 p.s我不想使用第三方底部导航。Android 单击图标时,如何在底部导航视图中禁用动画?,android,android-animation,bottomnavigationview,Android,Android Animation,Bottomnavigationview,我正在为我的应用程序使用默认的底部视图导航栏,它有4个按钮,它们有一个可怕的移动动画,而且compat库中似乎没有一个方法。禁用它。请帮忙 p.s我不想使用第三方底部导航。//创建一个新的类底部导航栏帮助器,然后在按钮类中实现它 //Create a new class Bottom Navigation Bar helper and then implement it in the buttons classes public class BottomNavigationViewHelper
//创建一个新的类底部导航栏帮助器,然后在按钮类中实现它
//Create a new class Bottom Navigation Bar helper and then implement it in the buttons classes
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) {
Log.e("BNVHelper", "Unable to get shift mode field", e);
} catch (IllegalAccessException e) {
Log.e("BNVHelper", "Unable to change value of shift mode", e);
}
}
//and then just use this code in every button class
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity);
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavViewBar);
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
BottomNavigationViewHelper.enableNavigation(mContext, bottomNavigationView);
Menu menu=bottomNavigationView.getMenu();
MenuItem menuItem=menu.getItem(ACTIVITY_NUM);
menuItem.setChecked(true);
}
公共类BottomNavigationViewHelper{
@SuppressLint(“RestrictedApi”)
公共静态void disableShiftMode(底部导航视图){
BottomNavigationMenuView menuView=(BottomNavigationMenuView)视图;
试一试{
Field shiftingMode=menuView.getClass().getDeclaredField(“MSShiftingMode”);
换档模式。设置可访问(真);
shiftingMode.setBoolean(菜单视图,false);
换档模式。设置可访问(错误);
对于(int i=0;i
这将使按钮运行良好。
我是android新手,代码不是我写的,但我希望它能帮助你看看你能通过编辑这个问题发布你的代码吗?@AswinPAshok谢谢,它起了作用:)对于那些发现这个问题的人,这里是安卓支持库问题追踪器中的一个问题,将它添加为BottomNavigationView的一个功能:如果你使用它,请检查这个API版本>=28。基本上,他们已经从BottomNavigationBar中删除了MSShiftingMode变量,因此我们必须以不同的方式对其进行处理。