1 方法一,文字图标在xml规划好,文字定义在string.xml,图片直接从工程文件中调用,至于所有的文字图片排版,放在单独的menu.xml文件中
1 | @Override |
2 | public boolean onCreateOptionsMenu(Menu menu) { |
3 | super .onCreateOptionsMenu(menu); |
4 | MenuInflater menuInflater = getMenuInflater(); |
5 | menuInflater.inflate(R.menu.menu, menu); |
6 | return true ; |
7 | } |
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | < menu xmlns:android = "http://schemas.android.com/apk/res/android" > |
03 | < item android:icon = "@android:drawable/ic_menu_mylocation" |
04 | android:id = "@+id/centerOnLocationMenuItem" android:title = "@string/myLocation" /> |
05 | < item android:icon = "@android:drawable/ic_menu_more" android:id = "@+id/chooseStop" |
06 | android:title = "@string/chooseStop" /> |
07 | < item android:icon = "@android:drawable/ic_menu_mylocation" |
08 | android:id = "@+id/centerOnBostonMenuItem" android:title = "@string/bostonsLocation" /> |
09 | < item android:icon = "@android:drawable/ic_menu_preferences" |
10 | android:id = "@+id/settingsMenuItem" android:title = "@string/settings" /> |
11 | < item android:icon = "@android:drawable/ic_menu_more" android:id = "@+id/chooseRoute" |
12 | android:title = "@string/chooseRoute" /> |
13 | < item android:icon = "@android:drawable/ic_menu_rotate" android:id = "@+id/refreshItem" |
14 | android:title = "@string/refresh" /> |
15 | </ menu > |
2,动态创建菜单
01 | @Override |
02 | public boolean onCreateOptionsMenu(Menu menu) { |
03 | // TODO Auto-generated method stub |
04 | menu.add( 0 , LOCATION, 1 , R.string.location); |
05 | menu.add( 0 ,SEARCH, 2 ,R.string.search); |
06 | menu.add( 0 , SHOWROUTE, 3 , R.string.vechicleLocation); |
07 | menu.add( 0 , ALLROUTE, 4 , R.string.allRoute); |
08 | return super .onCreateOptionsMenu(menu); |
09 | } |
10 | |
11 | public boolean onCreateOptionsMenu(Menu menu) |
12 | { |
13 | menu.add( 0 , CLASSIC_MENU_REFRESH, 0 , R.string.menu_refresh).setIcon(R.drawable.cmcc_toolbar_refresh); |
14 | return super .onCreateOptionsMenu(menu); |
15 | } |