您的位置:首页 > 移动开发 > Android开发

Android菜单设计(1) : 使用xml文件布局创建 options menu

2015-01-31 21:22 495 查看
1. 准备工作

下载几张图片,为menu锦上添花,推荐一个图片网站:http://www.easyicon.cn/

2. 新建android项目

在项目的res文件下面建立一个名称为menu的文件夹,用来放置xml文件。即menu的布局文件。

目录结构如下,所示:



game_menu.xml源码:

[xhtml]
view plaincopyprint?

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/new_game"
android:icon="@drawable/ic_new_game"
android:title="new_game" />

<item android:id="@+id/help"
android:icon="@drawable/ic_help"
android:title="help" />
</menu>

update_menu.xml源码:

[xhtml]
view plaincopyprint?

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/new_game"
android:icon="@drawable/back_game"
android:title="exit" />

<item android:id="@+id/help"
android:icon="@drawable/exit_game"
android:title="back" />
</menu>

3. 创建菜单

重写 onCreateOptionsMenu(Menu menu) 方法即可。

[java]
view plaincopyprint?

@Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.d(TAG, "onCreateOptionsMenu() is involed! " + (times++) + " th");
MenuInflater mInflater = getMenuInflater();
mInflater.inflate(R.menu.game_menu, menu);
// 等效下面代码
//return super.onCreateOptionsMenu(menu);
return true;// 返回false就不会显示菜单
}

ok,运行程序,点击"menu",显示效果如下:



4. 为菜单设计点击事件

重写 onOptionsItemSelected(MenuItem item) 方法即可!

[java]
view plaincopyprint?

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.new_game:
// newGame();
return true;
case R.id.help:
// showHelp();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

5. 动态改变menu

onCreateOptionsMenu(Menu menu) 方法只会在App运行时被调用一次,相当于Activity 的 onCreate( ) 方法类似。如果想动态改变menu,需要重写 onPrepareOptionsMenu(Menu menu) 方法。

[java]
view plaincopyprint?

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
Log.d(TAG, "onPrepareOptionsMenu() is involed! " + (times++) + " th");
MenuInflater mInflater = getMenuInflater();
mInflater.inflate(R.menu.update_menu, menu);
// 等效下面代码
//return super.onPrepareOptionsMenu(menu);
return true;// 返回false就不会显示菜单
}

显示效果,如下所示:



看看,log日志吧??!!!



ok,至此你明白了,onPrepareOptionsMenu(Menu menu) 方法在 onCreateOptionsMenu(Menu menu) 方法之后被调用,接着我们再次点击menu,多点击几次。效果和Log日志,如下所示。





可以发现,onPrepareOptionsMenu(Menu menu) 方法每次点击 menu 都会被调用一次。如果,在实际代码中这样做的话,会产生很多menu项,显然不可以,那怎么办呢?很简单,看示例代码如下:

[java]
view plaincopyprint?

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
Log.d(TAG, "onPrepareOptionsMenu() is involed! " + (times++) + " th");
// 清除menu
menu.clear();
MenuInflater mInflater = getMenuInflater();
mInflater.inflate(R.menu.update_menu, menu);
// 等效下面代码
//return super.onPrepareOptionsMenu(menu);
return true;// 返回false就不会显示菜单
}

这样的话,原先在 onCreateOptionsMenu(Menu menu) 方法中创建的菜单就会烟消云散,呵呵,效果图:



6. 提示

在最后,用sdk的api文档上面的原文做个提示:

On Android 2.3 and lower, the system calls onPrepareOptionsMenu() each time the user opens the Options Menu.
On Android 3.0 and higher, you must call invalidateOptionsMenu() when you want to update the menu, because the menu is always open. The system will then call onPrepareOptionsMenu() so you can update the menu items.

更多内容请参考你的sdk的api文档:docs/guide/topics/ui/menus.html#options-menu

转自:http://blog.csdn.net/veryitman/article/details/6577845
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐