Android-动态添加底部导航视图 [英] Android - Add bottom navigation view dynamically

查看:160
本文介绍了Android-动态添加底部导航视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试动态添加底部导航视图.我知道我在活动的xml文件中添加了一个导航视图.

I try to add a bottom navigation view dynamically. I know that I add a navigation view inside activity's xml file.

<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文件.我使用下面的代码创建导航栏.

I don't want to create a item xml file. I used below code to create navigation bar.

    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引发错误.

menu.add throws an error.

在空对象引用上

android.support.v7.view.menu.MenuBuilder.size()'

android.support.v7.view.menu.MenuBuilder.size()' on a null object reference

如何动态添加导航视图?

How can I add a navigation view dynamically?

推荐答案

这是BottomNavigationView的错误.

以下是错误参考: https://issuetracker.google.com/issues/37124043

此问题已在支持库25.0.1中修复.更新您的support library,然后重试.

This has been fixed in support library 25.0.1. Update your support library and try again.

希望这会有所帮助〜

这篇关于Android-动态添加底部导航视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆