菜单在操作栏的Android [英] Menu in Action bar Android
问题描述
我已经创建了动作条与标签动态,如低于code的一些数字。
I have created ActionBar with some number of tabs dynamically like the below code.
public void addTabBar(Context context)
{
sActiveContext=context;
sActionBar = getActionBar();
sActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
}
public void addTabItem(final String url, String tabTitle)
{
arrayList.add(url);
Tab tab = sActionBar.newTab();
if(tabTitle.equals(""))
{
int childcount=sActionBar.getTabCount();
tabTitle="Tab" + String.valueOf(childcount+1);
}
tab.setText(tabTitle);
tab.setTabListener(this);
sActionBar.addTab(tab);
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
linearLayout=new LinearLayout(sActiveContext);
linearLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
linearLayout.setOrientation(LinearLayout.VERTICAL);
setContentView(linearLayout);
CustomWebView webview=new CustomWebView(sActiveContext);
FrameLayout layout=webview.createwebview();
for (int i = 0; i < arrayList.size(); i++) {
if(tab.getPosition()==i)
{
webview.initwebview(arrayList.get(i));
break;
}
}
linearLayout.addView(layout);
}
如果我已经转换这个code的库,并调用这些方法,我可以创建标签的操作栏中的n个。现在,我想添加菜单项和下拉菜单(椭圆形有三个点),如在下面的链接。 http://developer.android.com/guide/topics/ui/actionbar html的#标签 如果我传递图像给showMenu(R.drawable.menu_image)的方法,所述菜单项都被动态创建。我怎样才能做到这一点?请给一些建议。
If I have converted this code as library and call those methods and I can create n number of tabs in action bar. Now, I wish to add menu items and dropdown menu(ellipse with three dots) like in the link below. http://developer.android.com/guide/topics/ui/actionbar.html#Tabs If I am passing the image to the showMenu(R.drawable.menu_image) method, the menu items have to be dynamically created. How can I achieve this? Please give some suggestions.
推荐答案
试试这个, 在你的活动,
Try this, In your Activity,
private Menu menu=null;
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
this.menu=menu;
// call here as, showMenu(R.drawable.menu_image,2222);
return true;
}
private void showMenu(int imageIcon,int id)//Where imageIcon is R.drawable.menu_image
{
menu.add(0, id, 0, actionString).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);;
MenuItem item=menu.findItem(id);
item.setIcon(imageIcon);
}
这篇关于菜单在操作栏的Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!