Android 自定义系统菜单的背景源码
01 | package lab.sodino.menutest; |
02 | import |
03 | android.content.Context; |
04 | import android.app.Activity; |
05 | import android.os.Bundle; |
06 | import android.os.Handler; |
07 | import android.util.AttributeSet; |
08 | import android.view.InflateException; |
09 | import android.view.LayoutInflater; |
10 | import android.view.Menu; |
11 | import android.view.MenuInflater; |
12 | import android.view.MenuItem; |
13 | import android.view.View; |
14 | import android.widget.Toast; |
15 | /** |
16 | * @author Sodino E-mail:sodinoopen@hotmail.com |
17 | * @version Time:2011-1-26 下午04:42:04 |
18 | */ |
19 | public class MenuAct extends Activity { |
20 | @Override |
21 | public void onCreate(Bundle savedInstanceState) { |
22 | super .onCreate(savedInstanceState); |
23 | setContentView(R.layout.main); |
24 | } |
25 | public boolean onCreateOptionsMenu(Menu menu) { |
26 | super .onCreateOptionsMenu(menu); |
27 | MenuInflater inflater = new MenuInflater(getApplicationContext()); |
28 | inflater.inflate(R.menu.menu, menu); |
29 | setMenuBackground(); |
30 | return true ; |
31 | } |
32 | public boolean onOptionsItemSelected(MenuItem item) { |
33 | String info = "" ; |
34 | switch (item.getItemId()) { |
35 | case R.id.menu_add: |
36 | info = "Add" ; |
37 | break ; |
38 | case R.id.menu_delete: |
39 | info = "Delete" ; |
40 | break ; |
41 | case R.id.menu_home: |
42 | info = "Home" ; |
43 | break ; |
44 | case R.id.menu_help: |
45 | info = "Help" ; |
46 | break ; |
47 | default : |
48 | info = "NULL" ; |
49 | break ; |
50 | } |
51 | Toast toast = Toast.makeText( this , info, Toast.LENGTH_SHORT); |
52 | toast.show(); |
53 | return super .onOptionsItemSelected(item); |
54 | } |
55 | // 关键代码为重写Layout.Factory.onCreateView()方法自定义布局 |
56 | protected void setMenuBackground() { |
57 | MenuAct. this .getLayoutInflater().setFactory( new android.view.LayoutInflater.Factory() { |
58 | /** |
59 | * name - Tag name to be inflated.<br/> |
60 | * context - The context the view is being created in.<br/> |
61 | * attrs - Inflation attributes as specified in XML file.<br/> |
62 | */ |
63 | public View onCreateView(String name, Context context, AttributeSet attrs) { |
64 | // 指定自定义inflate的对象 |
65 | if (name.equalsIgnoreCase( "com.android.internal.view.menu.IconMenuItemView" )) { |
66 | try { |
67 | LayoutInflater f = getLayoutInflater(); |
68 | final View view = f.createView(name, null , attrs); |
69 | new Handler().post( new Runnable() { |
70 | public void run() { |
71 | // 设置背景图片 |
72 | view.setBackgroundResource(R.drawable.menu_background); |
73 | } |
74 | }); |
75 | return view; |
76 | } catch (InflateException e) { |
77 | e.printStackTrace(); |
78 | } catch (ClassNotFoundException e) { |
79 | e.printStackTrace(); |
80 | } |
81 | } |
82 | return null ; |
83 | } |
84 | }); |
85 | } |
86 | } |
/res/menu/menu.xml
1 | <?xml version= "1.0" encoding= "utf-8" ?> |
2 | <menu xmlns:android= "http://schemas.android.com/apk/res/android" > |
3 | <item android:id= "@+id/menu_add" android:title= "Add" android:icon= "@drawable/menu_add" ></item> |
4 | <item android:id= "@+id/menu_delete" android:title= "Delete" android:icon= "@drawable/menu_delete" ></item> |
5 | <item android:id= "@+id/menu_home" android:title= "Home" android:icon= "@drawable/menu_home" ></item> |
6 | <item android:id= "@+id/menu_help" android:title= "Help" android:icon= "@drawable/menu_help" ></item> |
7 | </menu> |
转自:http://www.eoeandroid.com/code/2012/0803/2007.html