Android:一项活动中有多个选项菜单 [英] Android: Multiple Option Menus in one Activity
问题描述
我有一个 Activity
,其中包含一个 ViewFlipper
,并且想为该 ViewFlipper
中的每个视图显示不同的选项菜单.也就是说,当按下菜单按钮时显示的菜单类型将取决于当前视图的类型.但是, onCreateOptionsMenu()
仅被调用一次(第一次显示选项菜单时),因此无法在此处实现创建不同的菜单.我该怎么解决?
I have an Activity
containing a ViewFlipper
and would like to show a different option menu for each view in that ViewFlipper
. That is, the type of menu displayed when the menu button is pressed would depend on the type of the current view. However, onCreateOptionsMenu()
is called only once (when showing the option menu for the first time), so creating the different menus can't be implemented there. How could I solve this?
任何建议表示赞赏.
推荐答案
Each time the user presses the Menu on their Android device while inside one of your activities, the onPrepareOptionsMenu
method is called. The first time the menu is shown (i.e. only once), the onCreateOptionsMenu
method is called.
基本上,您应该在 onPrepareOptionsMenu
方法中进行任何更改,例如启用/禁用某些菜单项,或根据情况更改菜单项文本.
Basically, the onPrepareOptionsMenu
method is where you should make any changes such as enabling/disabling certain menu items, or changing the menu item text depending on the circumstances.
这样做(不要使用 onCreateOptionsMenu(菜单菜单)
)
//Dynamically create context Menu
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear(); //Clear view of previous menu
MenuInflater inflater = getMenuInflater();
if(condition_true)
inflater.inflate(R.menu.menu_one, menu);
else
inflater.inflate(R.menu.menu_two, menu);
return super.onPrepareOptionsMenu(menu);
}
这篇关于Android:一项活动中有多个选项菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!