本文介绍了如何动态向底部导航视图添加菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Android具有新的ui元素-BottomNavigationView
Android has new ui element - BottomNavigationView
我不想在xml文件中包含菜单.我将从后端接收有关菜单项和订单的信息.我想动态创建它们,并在onCreate()
方法中将其设置为BottomNavigationView
.我可以这样做吗?
I don't want to contain my menus in the xml files. I will receive the information about menu items and order from backend side. I want to create them dynamically and set into the BottomNavigationView
in the onCreate()
method. Can I do this?
推荐答案
默认情况下,BottomNavigationView
以一个空菜单开头.您可以使用 getMenu()
方法来获取Menu实例,然后按照上面的响应添加菜单项.例如,
By default, BottomNavigationView
starts with an empty menu. You can use the getMenu()
method to get the Menu instance, then add menu items as in the response above. For example,
BottomNavigationView bottomNavigation = findViewById(R.id.bottom_navigation);
Menu menu = bottomNavigation.getMenu();
menu.add(Menu.NONE, MENU_ITEM_ID_ONE, Menu.NONE, getString(R.string.str_menu_one))
.setIcon(R.drawable.ic_action_one);
这篇关于如何动态向底部导航视图添加菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!