Android 中的 BottomNavigationView 中的选项卡不对称
Tabs are not get symmetrical in BottomNavigationView in Android
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 component
<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" />
when I added 3 tabs BottomNavigationView got symmetrical.but when it's come to 4 tabs BottomNavigationView not symmetrical.how can I solve that? I really appreciate your suggestions.Thanks in advance.
移除 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);
}
}
来源
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 component
<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" />
when I added 3 tabs BottomNavigationView got symmetrical.but when it's come to 4 tabs BottomNavigationView not symmetrical.how can I solve that? I really appreciate your suggestions.Thanks in advance.
移除 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);
}
}
来源