我们如何动态添加菜单项 [英] how we can add menu item dynamically
问题描述
您好,我正在创建一个应用程序,它是一个标签应用程序.
hi frnds am creating an application which is a tab application.
在我的扩展sherlockFragmentActivity的主页中,我正在膨胀menu.xml并包括optionMenuitem单击侦听器上的代码. Fragmentactivity包含tabhost,并在每个选项卡上加载片段. 这是我的menu.xml
in my Home which extends sherlockFragmentActivity, i am inflating menu.xml and includes code for on optionMenuitem click listener. The Fragmentactivity contains tabhost and on each tab it load fragments. this is my menu.xml
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:showAsAction="always"
android:icon="@drawable/setting_selector"
android:title=""
>
<menu >
<item
android:id="@+id/Profile"
android:showAsAction="ifRoom"
android:title="Profile"/>
<item
android:id="@+id/chngDoctor"
android:showAsAction="ifRoom"
android:title="Change doctor"
android:visible="false"/>
<item
android:id="@+id/changePword"
android:showAsAction="ifRoom"
android:title="Change password"/>
<item
android:id="@+id/logout"
android:showAsAction="ifRoom"
android:title="Logout"/>
</menu>
</item>
这是类Home中的onCreateOptionMenu和onOptionItemSelected方法
and this is my onCreateOptionMenu and onOptionItemSelected methods in class Home
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getSupportMenuInflater().inflate(R.menu.main, menu);
SubMenu subMenu = (SubMenu) menu.getItem(0).getSubMenu();
if(userType.equals("admin"))
subMenu.getItem(1).setVisible(true);
else
subMenu.getItem(1).setVisible(false);
return true;
}
这是我的onOptionItemSelected方法
and this is my onOptionItemSelected method
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId()) {
case R.id.Profile:
break;
case R.id.changePword :
break;
case R.id.chngDoctor :
break;
case R.id.logout:
Home.this.finish();
break;
}
return true;
}
我需要根据选项卡的更改添加一些菜单.在选项卡更改上,我加载了不同的片段,当片段更改时,我需要向菜单中添加新项目.我的ListFrag扩展了SherlockFragment,当我单击第3个选项卡时它将加载.当此片段加载时,我需要在菜单中添加1个菜单项
i need to add some menus depending on tab change. that is on tab change i load different fragments and when fragment changes i need to add new items to the menu. my ListFrag which extends SherlockFragment and it will load when i click on the 3 rd tab. when this fragment load i need to add 1 menu item to the menu
推荐答案
尝试以下方法.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, 0, 0, "Option1").setShortcut('3', 'c');
menu.add(0, 1, 0, "Option2").setShortcut('3', 'c');
menu.add(0, 2, 0, "Option3").setShortcut('4', 's');
SubMenu sMenu = menu.addSubMenu(0, 3, 0, "SubMenu"); //If you want to add submenu
sMenu.add(0, 4, 0, "SubOption1").setShortcut('5', 'z');
sMenu.add(0, 5, 0, "SubOption2").setShortcut('5', 'z');
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 0:
// code for option1
return true;
case 1:
// code for option2
return true;
case 2:
// code for option3
return true;
case 4:
// code for subOption1
return true;
case 5:
// code for subOption2
return true;
}
return super.onOptionsItemSelected(item);
}
这可能会对您有所帮助.
This may help you.
这篇关于我们如何动态添加菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!