我们传统意义上 的定义菜单感觉比较繁琐,当我们使用MenuInflater 来生成菜单,你会发现是多么的爽朗,呵呵,我今天的小Demo,是定义四个菜单,并且实现了一个菜单事件。就是我们点击设置 (Setting )菜单,进入手机设置状态!下面看一下效果图:
下面是实现Demo 的详细步骤:
一、建立一个Android 工程我们命名为MenuInflaterDemo .
二、在res 目录下创建menu 目录,并且创建options_menu.xml (我们定义的菜单)文件,代码如下:
Java代码:
1. <?xml version="1.0" encoding="utf-8"?>
2. <menu
3. xmlns:android="http://schemas.android.com/apk/res/android">
4. <item android:id="@+id/menu_add"
5. android:title="Add"
6. android:icon="@android:drawable/ic_menu_add"
7. />
8. <item android:id="@+id/menu_wallaper"
9. android:title="Wallpaper"
10. android:icon="@android:drawable/ic_menu_gallery"
11. />
12. <item android:id="@+id/menu_search"
13. android:title="Search"
14. android:icon="@android:drawable/ic_search_category_default"
15. />
16. <item android:id="@+id/menu_setting"
17. android:title="Settings"
18. android:icon="@android:drawable/ic_menu_preferences"
19. />
20. </menu>
三、主类MenuInflaterDemo.java 的编码,这里写的代码很少哦,我这里只写了第四个菜单(Settings )的响应事件.全部代码如下:
Java代码:
1. package eoe.demo;
2. import android.app.Activity;
3. import android.content.Intent;
4. import android.os.Bundle;
5. import android.view.Menu;
6. import android.view.MenuInflater;
7. import android.view.MenuItem;
8. public class MenuInflaterDemo extends Activity {
9. @Override
10. public void onCreate(Bundle savedInstanceState) {
11. super.onCreate(savedInstanceState);
12. setContentView(R.layout.main);
13. }
14. @Override
15. public boolean onCreateOptionsMenu(Menu menu) {
16. MenuInflater inflater = getMenuInflater();
17. inflater.inflate(R.menu.options_menu, menu);
18. return true;
19. }
20. @Override
21. public boolean onOptionsItemSelected(MenuItem item) {
22. switch (item.getItemId()) {
23. case R.id.menu_add:
24. break;
25. case R.id.menu_wallaper:
26. break;
27. case R.id.menu_search:
28. break;
29. case R.id.menu_setting:
30. showSettings();
31. break;
32. }
33. return super.onOptionsItemSelected(item);
34. }
35. private void showSettings(){
36. final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
37. settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
38. Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
39. startActivity(settings);
40. }
41. }
四、运行代码之,点击模拟器上的menu 按钮将会出现上述效果图!
- public class MenuInflaterTest extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- }
- public boolean onCreateOptionsMenu(Menu menu) {
- // 获取当前的菜单
- MenuInflater inflater = getMenuInflater();
- // 填充菜单
- inflater.inflate(R.menu.option_menu, menu);
- return true;
- }
- /**
- * 对菜单点击事件处理
- */
- public boolean onOptionsItemSelected(MenuItem item) {
- switch (item.getItemId()) {
- case R.id.menu_add:
- break;
- case R.id.menu_wallaper:
- break;
- case R.id.menu_search:
- break;
- case R.id.menu_setting:
- showSettings();
- break;
- }
- return super.onOptionsItemSelected(item);
- }
- /**
- * 显示设置选项
- */
- private void showSettings() {
- Intent settings = new Intent
- (android.provider.Settings.ACTION_SETTINGS);
- settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
- | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
- startActivity(settings);
- }
- }
- }
public class MenuInflaterTest extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public boolean onCreateOptionsMenu(Menu menu) {
// 获取当前的菜单
MenuInflater inflater = getMenuInflater();
// 填充菜单
inflater.inflate(R.menu.option_menu, menu);
return true;
}
/**
* 对菜单点击事件处理
*/
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_add:
break;
case R.id.menu_wallaper:
break;
case R.id.menu_search:
break;
case R.id.menu_setting:
showSettings();
break;
}
return super.onOptionsItemSelected(item);
}
/**
* 显示设置选项
*/
private void showSettings() {
Intent settings = new Intent
(android.provider.Settings.ACTION_SETTINGS);
settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
startActivity(settings);
}
}
}
Main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- </LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
Option_menu.xml
- <?xml version="1.0" encoding="utf-8"?>
- <menu xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:id="@+id/menu_add"
- android:title="Add"
- android:icon="@android:drawable/ic_menu_add"/>
- <item android:id="@+id/menu_wallaper"
- android:title="Wallpaper"
- android:icon="@android:drawable/ic_menu_gallery"/>
- <item android:id="@+id/menu_search"
- android:title="Search"
- android:icon="@android:drawable/ic_search_category_default"/>
- <item android:id="@+id/menu_setting"
- android:title="Settings"
- android:icon="@android:drawable/ic_menu_preferences"/> </menu>