Android有新的ui元素 - BottomNavigationView
我不想在xml文件中包含我的菜单。我将从后端收到有关菜单项和订单的信息。我想动态创建它们并设置为BottomNavigationView
方法中的onCreate()
。我可以这样做吗?
答案 0 :(得分:10)
默认情况下,BottomNavigationView
以空菜单开头。您可以使用getMenu()
方法获取Menu实例,然后添加response above中的菜单项。例如,
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);
答案 1 :(得分:6)
在底部导航视图中使用动态选项的简便方法是使用不同的菜单项:
switch (userType){
case UserTypes.A:
bottomNavigationView.inflateMenu(R.menu.menu_bottom_navigation_a);
break;
case UserTypes.B:
bottomNavigationView.inflateMenu(R.menu.menu_bottom_navigation_b);
break;
case UserTypes.C:
bottomNavigationView.inflateMenu(R.menu.menu_bottom_navigation_c);
break;
}
答案 2 :(得分:4)
下面是动态构建菜单项的示例,在主要活动中,您有2个布局。 您可以从后端获取自己的布局,并使用menu.add
动态创建菜单项src:http://www.mobiledevguide.com/2014/01/dynamically-create-menu-items-in-android.html
public class MainActivity extends Activity {
private Button mButtonOne,mButtonTwo;
private static final int MENU_ITEM_ID_ONE =1;
private static final int MENU_ITEM_ID_TWO =2;
private static final int MENU_ITEM_ID_THREE =3;
private static final int MENU_ITEM_ID_FOUR =4;
private static final int MENU_ITEM_ID_FIVE =5;
private static final int MENU_ITEM_ID_SIX =6;
private int mMenuSet = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButtonOne=(Button) findViewById(R.id.buttonSetOne);
mButtonTwo=(Button) findViewById(R.id.buttonSetTwo);
mButtonOne.setOnClickListener(clickListener);
mButtonTwo.setOnClickListener(clickListener);
}
OnClickListener clickListener=new OnClickListener() {
@Override
public void onClick(View v) {
if (v.getId()==R.id.buttonSetOne) {
mMenuSet=1;
} else if (v.getId()==R.id.buttonSetTwo){
mMenuSet=2;
}
invalidateOptionsMenu();
/*
* if you are using ActionBarSherlock use this.supportInvalidateOptionsMenu();
* if you are using ActionBarCompat use invalidateOptionsMenu (Activity activity) method
* */
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
if(mMenuSet==1){
menu.add(Menu.NONE, MENU_ITEM_ID_ONE, Menu.NONE,getString(R.string.str_menu_one)).setIcon(R.drawable.ic_action_one).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(Menu.NONE, MENU_ITEM_ID_TWO, Menu.NONE,getString(R.string.str_menu_two)).setIcon(R.drawable.ic_action_two).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(Menu.NONE, MENU_ITEM_ID_THREE, Menu.NONE,getString(R.string.str_menu_three)).setIcon(R.drawable.ic_action_three);
}else if(mMenuSet==2){
menu.add(Menu.NONE, MENU_ITEM_ID_FOUR, Menu.NONE,getString(R.string.str_menu_four)).setIcon(R.drawable.ic_action_four).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(Menu.NONE, MENU_ITEM_ID_FIVE, Menu.NONE,getString(R.string.str_menu_five)).setIcon(R.drawable.ic_action_five).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(Menu.NONE, MENU_ITEM_ID_SIX, Menu.NONE,getString(R.string.str_menu_six)).setIcon(R.drawable.ic_action_six);
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_ITEM_ID_ONE:
Toast.makeText(this, "Click on "+ getString(R.string.str_menu_one), Toast.LENGTH_SHORT).show();
break;
case MENU_ITEM_ID_TWO:
Toast.makeText(this, "Click on "+ getString(R.string.str_menu_two), Toast.LENGTH_SHORT).show();
break;
case MENU_ITEM_ID_THREE:
Toast.makeText(this, "Click on "+ getString(R.string.str_menu_three), Toast.LENGTH_SHORT).show();
break;
case MENU_ITEM_ID_FOUR:
Toast.makeText(this, "Click on "+ getString(R.string.str_menu_four), Toast.LENGTH_SHORT).show();
break;
case MENU_ITEM_ID_FIVE:
Toast.makeText(this, "Click on "+ getString(R.string.str_menu_five), Toast.LENGTH_SHORT).show();
break;
case MENU_ITEM_ID_SIX:
Toast.makeText(this, "Click on "+ getString(R.string.str_menu_six), Toast.LENGTH_SHORT).show();
break;
case R.id.action_settings:
Toast.makeText(this, "Click on "+ getString(R.string.action_settings), Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}}