2.在 res 目錄下創建 menu 目錄,並且創建 options_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>
3.在主要程式碼內加入,如下
@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.options_menu, menu); return true; } @Override 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: break ; } return super .onOptionsItemSelected(item); }
當選單選到Add or Wallpaper ...則會執行 onOptionsItemSelected 底下
switch 的case
參考網站:http://blog.csdn.net/Android_Tutor/archive/2010/04/23/5522913.aspx
沒有留言:
張貼留言