Android:How 在菜单选项中使用 Sub_menu 类别
Android:How To use Sub_menu category in menu options
在我的 Activity 中,当用户单击另一个选项菜单 (Sub_menu) 想要的文本选项时,我放置了一个选项菜单,如 文本选项 加载文本颜色和文本大小调整我们如何在Android中做到这一点请帮助我是新手Android
让您的 menu_activity.xml
拥有您想要的所有 menu item
。如果您最初不想显示 menu item
,则将其可见性设置为 false。 android:visible="false"
<item
android:id="@+id/text_options"
android:orderInCategory="2"
android:title="@string/text_options"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/text_color"
android:orderInCategory="3"
android:title="@string/text_color"
android:visible="false"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/text_size"
android:orderInCategory="4"
android:title="@string/text_size"
android:visible="false"
app:showAsAction="ifRoom"/>
在你的Activity
中有以下方法:
private boolean isShown = true;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_activity, menu);
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id.text_options).setVisible(!isShown);
menu.findItem(R.id.text_color).setVisible(isShown);
menu.findItem(R.id.text_size).setVisible(isShown);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.text_options:
hideMenu();
}
return super.onOptionsItemSelected(item);
}
public void hideMenu() {
isShown = false;
invalidateOptionsMenu();
}
public void showMenu() {
isShown = true;
invalidateOptionsMenu();
}
调用invalidateOptionsMenu()
后,菜单列表将重新计算,即调用onPrepareOptionsMenu()
。稍微修改一下,你就可以实现你想要的。
更新:
如果您想添加 sub_menu,请将您的 menu_activity.xml
修改为如下所示:
You can add a submenu to an item in any menu (except a submenu) by
adding a element as the child of an . Submenus are useful
when your application has a lot of functions that can be organized
into topics, like items in a PC application's menu bar (File, Edit,
View, etc.). For example:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/file"
android:title="@string/file" >
<!-- "file" submenu -->
<menu>
<item android:id="@+id/create_new"
android:title="@string/create_new" />
<item android:id="@+id/open"
android:title="@string/open" />
</menu>
</item>
</menu> To use the menu in your activity, you need to inflate the menu resource (convert the XML resource into a programmable object)
using MenuInflater.inflate(). In the following sections, you'll see
how to inflate a menu for each menu type.
更多信息在这里:http://developer.android.com/guide/topics/ui/menus.html
在我的 Activity 中,当用户单击另一个选项菜单 (Sub_menu) 想要的文本选项时,我放置了一个选项菜单,如 文本选项 加载文本颜色和文本大小调整我们如何在Android中做到这一点请帮助我是新手Android
让您的 menu_activity.xml
拥有您想要的所有 menu item
。如果您最初不想显示 menu item
,则将其可见性设置为 false。 android:visible="false"
<item
android:id="@+id/text_options"
android:orderInCategory="2"
android:title="@string/text_options"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/text_color"
android:orderInCategory="3"
android:title="@string/text_color"
android:visible="false"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/text_size"
android:orderInCategory="4"
android:title="@string/text_size"
android:visible="false"
app:showAsAction="ifRoom"/>
在你的Activity
中有以下方法:
private boolean isShown = true;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_activity, menu);
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id.text_options).setVisible(!isShown);
menu.findItem(R.id.text_color).setVisible(isShown);
menu.findItem(R.id.text_size).setVisible(isShown);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.text_options:
hideMenu();
}
return super.onOptionsItemSelected(item);
}
public void hideMenu() {
isShown = false;
invalidateOptionsMenu();
}
public void showMenu() {
isShown = true;
invalidateOptionsMenu();
}
调用invalidateOptionsMenu()
后,菜单列表将重新计算,即调用onPrepareOptionsMenu()
。稍微修改一下,你就可以实现你想要的。
更新:
如果您想添加 sub_menu,请将您的 menu_activity.xml
修改为如下所示:
You can add a submenu to an item in any menu (except a submenu) by adding a element as the child of an . Submenus are useful when your application has a lot of functions that can be organized into topics, like items in a PC application's menu bar (File, Edit, View, etc.). For example:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/file" android:title="@string/file" > <!-- "file" submenu --> <menu> <item android:id="@+id/create_new" android:title="@string/create_new" /> <item android:id="@+id/open" android:title="@string/open" /> </menu> </item> </menu> To use the menu in your activity, you need to inflate the menu resource (convert the XML resource into a programmable object)
using MenuInflater.inflate(). In the following sections, you'll see how to inflate a menu for each menu type.
更多信息在这里:http://developer.android.com/guide/topics/ui/menus.html