23

Android 有新的 ui 元素 - BottomNavigationView

在此处输入图像描述

我不想在 xml 文件中包含我的菜单。我将从后端收到有关菜单项和订单的信息。我想动态创建它们并设置到BottomNavigationView方法中onCreate()。我可以这样做吗?

4

3 回答 3

27

默认情况下,BottomNavigationView从一个空菜单开始。您可以使用该getMenu()方法获取 Menu 实例,然后按照上面的响应添加菜单项。例如,

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);
于 2017-09-24T18:45:02.483 回答
14

在底部导航视图中使用动态选项的简单方法是使用不同的菜单项,如下所示:

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;
        }
于 2018-02-12T10:47:46.147 回答
6

这是一个动态构建菜单项的示例,在主要活动中,您有 2 个布局。您可以从后端获取自己的布局并使用 menu.add 动态创建菜单项

来源: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);
}}
于 2016-12-27T12:24:43.117 回答