【发布时间】:2017-05-11 12:15:39
【问题描述】:
我尝试动态添加底部导航视图。我知道我在活动的 xml 文件中添加了一个导航视图。
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
design:menu="@menu/items" />
我不想创建项目 xml 文件。我使用下面的代码来创建导航栏。
bottomNavigationView = new BottomNavigationView(this);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
bottomNavigationView.setLayoutParams(params);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.viewLayout);
layout.addView(bottomNavigationView);
Menu menu = bottomNavigationView.getMenu();
menu.add(0, i, Menu.NONE, "TEXT");
menu.add 引发错误。
android.support.v7.view.menu.MenuBuilder.size()' 在空对象引用上
如何动态添加导航视图?
【问题讨论】:
-
您的支持库的版本是多少?
-
编译'com.android.support:design:25.0.0'
-
更新您的支持库并重试
标签: android menu bottomnavigationview