[英]more than 3 itesm in android bottom navigation menu using design support library
I am using android design support library V 25.0.0 to create bottom navigation menu.我正在使用 android 设计支持库 V 25.0.0 创建底部导航菜单。 i have configured 5 items in menu .
我在菜单中配置了 5 个项目。 but only 3 items are shown with title.
但只有 3 个项目显示有标题。 I want all 5 items to be shown with icons and title .
我希望所有 5 个项目都显示图标和标题。
is anyone able to achieve this?有没有人能够做到这一点?
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/action_search"
android:title="@string/menu_search"
android:icon="@drawable/ic_search" />
<item android:id="@+id/action_settings"
android:title="@string/menu_settings"
android:icon="@drawable/ic_add" />
....... three more items
layout file布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="horizontal"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="@color/colorPrimary"
app:itemIconTint="@color/white"
app:itemTextColor="@color/white"
app:menu="@menu/bottom_bar_tabs"
/>
</RelativeLayout>
Implementation of BottomNavigationView
has condition: when there is more than 3 items then use shift mode. BottomNavigationView
实现有条件:当超过 3 个项目时,使用 shift 模式。
At this moment you cannot change it through existing API and the only way to disable shift mode is to use reflection.目前您无法通过现有 API 更改它,禁用移位模式的唯一方法是使用反射。
You'll need helper class:你需要帮助类:
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);
}
}
}
And then apply disableShiftMode method on your BottomNavigationView, but remember if you are inflating menu view from your code, you have to execute it after inflating.然后在您的BottomNavigationView 上应用disableShiftMode 方法,但请记住,如果您从代码中膨胀菜单视图,则必须在膨胀后执行它。
Example usage:用法示例:
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation_bar);
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
Important重要的
Remember, you'll need to execute this method each time you change menu items in your BottomNavigationView.请记住,每次更改 BottomNavigationView 中的菜单项时都需要执行此方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.