定义一个xml文蚂旅件(在menu资源里面),
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_settings"
android:showAsAction="never"
android:title="设置" />
<item
android:id="@+id/menu_exit"
android:showAsAction="never"
android:title="退出"闷喊凳 />
</menu>
(item有多少个,显示就多少渗早个)
在代码里面的onCreateOptionsMenu方法里面把这个xml布局填充进去,代码如下:
MenuInflater inflater = getMenuInflater()
inflater.inflate(R.menu.menu_settings, menu)
在Launcher.java:private static final int MENU_GROUP_WALLPAPER = 1
/// M: menu group for settings and apps item, add for OP09.
private static final int MENU_GROUP_SETTINGS = 2
private static final int MENU_GROUP_APPS = 3
private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1
private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1
private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1
private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (isWorkspaceLocked()) {
return false
}
super.onCreateOptionsMenu(menu)
Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS)
manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS)
Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS)
settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED)
String helpUrl = getString(R.string.help_url)
Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl))
help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS)
menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
.setIcon(android.R.drawable.ic_menu_gallery)
.setAlphabeticShortcut('W')
menu.add(MENU_GROUP_APPS, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
.setIcon(android.R.drawable.ic_menu_manage)
.setIntent(manageApps)
.setAlphabeticShortcut('M')
menu.add(MENU_GROUP_SETTINGS, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
.setIcon(android.R.drawable.ic_menu_preferences)
.setIntent(settings)
.setAlphabeticShortcut('P')
if (!helpUrl.isEmpty()) {
menu.add(0, MENU_HELP, 0, R.string.menu_help)
.setIcon(android.R.drawable.ic_menu_help)
.setIntent(help)
.setAlphabeticShortcut('H')
}
return true
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu)
/// M: show options menu when in workspace state
if (mState == State.WORKSPACE &&!mWorkspace.isInOverviewMode()) {
mWorkspace.enterOverviewMode()
}else if (mState==State.APPS_CUSTOMIZE){
return true
}
return false
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)