2016-12-27 201 views
3

Android有新的UI元素 - BottomNavigationView如何將菜單動態添加到底部導航視圖?

enter image description here

我不想包含在XML文件中我的菜單。我會收到有關菜單項和後端訂購的信息。我想動態創建它們並將其設置爲onCreate()方法中的BottomNavigationView。我可以這樣做嗎?

回答

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); 
}} 
+1

問題是處理** ** BottomNavigationView的行爲。 onCreateOptionsMenu只能處理菜單。我知道如何動態創建菜單項。 – Malder

+0

你有沒有得到任何解決方案? – Sanal

+0

那麼..它的作品...但如何綁定點擊監聽器呢?請清楚這個細節,你會得到更多+1,老兄! :) –

1

默認情況下,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

在底部導航視圖中使用動態選項簡單的方法是使用不同的菜單項是這樣的:

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; 
     } 
相關問題