1

enter image description here标签并不是BottomNavigationView获得对称的安卓

navigation.xml

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:id="@+id/navigation_clock" 
     android:icon="@drawable/time" 
     android:title="" /> 

    <item android:id="@+id/navigation_medicine" 
     android:icon="@drawable/medicine" 
     android:title="" /> 

    <item android:id="@+id/navigation_user" 
     android:icon="@drawable/users" 
     android:title="" /> 

    <item android:id="@+id/navigation_setting" 
     android:icon="@drawable/setting" 
     android:title="" /> 

</menu> 

activity_mail.xml BottomNavigationView组件

<android.support.design.widget.BottomNavigationView android:id="@+id/navigation" 
     android:layout_width="0dp" android:layout_height="wrap_content" 
     android:layout_marginEnd="0dp" android:layout_marginStart="0dp" 
     android:background="?android:attr/windowBackground" 
     app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" app:menu="@menu/navigation" /> 

当我添加3个标签BottomNavigationView得到了symmetrical.but当它来到4个标签BottomNavigationView不symmetrical.how我可以解决这个问题吗?我非常感谢你的建议。提前感谢。

+1

您是否使用了矢量XML或绘制PNG? – GGWP

+0

我使用可绘制的PNG图标 – HRCJ

回答

2

为BottomNavigationView

navigation = (BottomNavigationView) findViewById(R.id.navigation); 
     disableShiftMode(navigation); 

方法删除默认的动画

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); 
     } 
    } 

https://stackoverflow.com/a/43491890/5423894

+0

您节省了我的一天。感谢您为这个真棒的答案。 – HRCJ