简体   繁体   English

使用设计支持库的 android 底部导航菜单中的 3 个以上项目

[英]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.

相关问题 使用android设计支持库的从右到左导航抽屉菜单 - Right to left navigation drawer menu using android design support library 使用android支持库和片段的底部导航 - Bottom navigation using android support library and fragment Android设计支持库是否比向后兼容? - Android Design Support Library More Than Backward Compatibility? Android设计支持导航抽屉在底部对齐 - Android design support navigation drawer align on bottom 如何使用 Android 设计支持库创建导航抽屉? - How to Create Navigation Drawer Using Android Design Support Library? 如何使用设计支持库在Android中的导航抽屉中添加页脚? - How to add footer to Navigation Drawer in Android using design support library? 使用android设计支持库将onClickListener添加到导航视图标题 - add onClickListener to navigation view header using android design support library 我从 Material Design for Android 的底部导航向我展示了比它应该多的一项 - My Bottom Navigation from Material Design for Android is showing me one item more than it should Android设计支持库辅助抽屉菜单 - Android Design Support Library Secondary Drawer Menu 底部导航栏android中超过3项 - More than 3 items in bottom navigation bar android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM