7

你好成员stackoverflow问题与bottomnavihationview

在我的应用程序中,我使用了带有 4 项的 BottomNavigationView。它使我的应用程序变得简单而美观

  BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation_view);
        bottomNavigationView.inflateMenu(R.menu.menu_bottom_navigation);
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                int id = item.getItemId();
                switch (id){
                    case R.id.action_one:

                        break;
                    case R.id.action_two:
                        FragmentTransaction manger= getSupportFragmentManager().beginTransaction();
                        pop_web_view  pop3 =new pop_web_view();
                        pop3.show(manger,null);

                        break;
                    case R.id.action_three:

                        break;
                    case R.id.action_four:

                        break;
                }

                return false;
            }
        });

在 activity_main :

<android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_anchorGravity="bottom"
        android:paddingTop="560dp"
        app:itemBackground="@color/colorDivider"
        app:itemIconTint="@color/colorPrimaryDark"
        app:itemTextColor="@color/colorPrimaryDark"
        app:menu="@menu/menu_bottom_navigation" />

在菜单 xml 中:

<?xml version="1.0" encoding="utf-8"?>
<menu
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/action_one"
        android:icon="@android:drawable/ic_secure"
        android:title="One"/>
    <item
        android:id="@+id/action_two"
        android:icon="@android:drawable/ic_dialog_info"
        android:title="Two"/>
    <item
        android:id="@+id/action_three"
        android:icon="@android:drawable/ic_dialog_email"
        android:title="Three"/>
    <item
        android:id="@+id/action_four"
        android:icon="@android:drawable/ic_popup_reminder"
        android:title="Four"/>
</menu>

但我有问题引起的:

java.lang.IllegalArgumentException: Maximum number of items supported by BottomNavigationView is 5. Limit can be checked with BottomNavigationView#getMaxItemCount()
4

3 回答 3

19

该错误表示支持的最大项目数BottomNavigationView为 5。

并尝试删除

bottomNavigationView.inflateMenu(R.menu.menu_bottom_navigation);

因为你已经在膨胀它了 app:menu="@menu/menu_bottom_navigation"

你正在通过调用来修改它

bottomNavigationView.inflateMenu(R.menu.menu_bottom_navigation);

文档说菜单中的现有项目不会被修改或删除。

检查此文档

并检查这个答案

BottomNavigationView 的实现有条件:当超过 3 个项目时使用移位模式。

于 2017-02-10T03:43:01.370 回答
4

我知道您接受了当前的答案,但它不完整。您在 XML 中添加了一个菜单到BottomNavigationView,然后您尝试调用inflateMenu(...),但是,文档清楚地表明:

菜单中的现有项目不会被修改或删除。

这意味着您正在向此视图添加菜单项,而不是替换它们。

您可以采取以下措施来解决它:

bottomNavigationView.getMenu().clear();
bottomNavigationView.inflateMenu(R.menu.menu_bottom_navigation);

另外值得一提的是,您做了两次相同的事情:一次在 XML 中(通过向app:menu="..."布局项添加属性),一次在 Java 类中通过调用inflateMenu(...)方法。删除其中任何一个,它都会起作用。请记住,如果您想稍后动态更改菜单项,则需要按照我发布的方式清除现有项目。

于 2017-02-10T04:22:06.380 回答
0

在我的应用程序中,我使用了带有 2 个项目的 BottomNavigationView,但有 4 个项目是这样的: 重复项目

我的 XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/bottom_navigation_constraintlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="0dp"
    android:layout_marginLeft="0dp"
    android:layout_marginRight="0dp"
    tools:context=".BottomNavigation">

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="@android:color/holo_red_dark"
        android:visibility="visible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/navigation" />

</android.support.constraint.ConstraintLayout>

和我的代码:

public class BottomNavigation extends AppCompatActivity {

    private static final String TAG = "BottomNavigation";

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        try {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_bottom_navigation);
            updateMenu();
        } catch (Exception e) {
            Log.e(TAG, "onCreate: ", e);;
        }
    }

    @UiThread
    private void updateMenu() {
        BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
        if (bottomNavigationView != null) {
            bottomNavigationView.inflateMenu(R.menu.navigation);

            for (Integer i = 0;
                 i < bottomNavigationView.getMenu().size();
                 ++i   ) {
                Log.i(TAG, "updateMenu: " + bottomNavigationView.getMenu().getItem(i).getItemId());
            }
        }
    }
}

删除其中任何一个,它都会起作用。谢谢。

于 2018-06-06T07:46:34.657 回答