如何给android中的设置菜单中添加一个item

如何给android中的设置菜单中添加一个item,第1张

在res下新建menu文件夹,然后新建菜单文件

定义一个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

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11996288.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存