涓?銆侀?夐」鑿滃崟鍜屽瓙鑿滃崟
鑿滃崟璧勬簮鏂囦欢涓?鑸?斁鍦?res/menu鐩?綍涓嬶紝鑿滃崟璧勬簮鐨勬牴鍏冪礌閫氬父鏄?lt;menu />,<menu />,鍏冪礌鏃犻渶鎸囧畾浠讳綍灞炴?э紝瀹冨彲浠ュ寘鍚?瓙鍏冪礌锛?
<item />:瀹氫箟鑿滃崟椤?
<group />:灏嗗?涓猧tem瀹氫箟鐨勮彍鍗曞寘瑁呮垚涓?涓?彍鍗曠粍銆?
涓?鑸?帹鑽愪娇鐢╔ML鏂囦欢瀹氫箟鑿滃崟銆?
eg.瀹氫箟鑿滃崟鐨勭粡鍏镐緥瀛?
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:title="@string/font_size" android:icon="@drawable/font"> <menu> <!-- 瀹氫箟涓?缁勫崟閫夎彍鍗曢」 --> <group android:checkableBehavior="single"> <!-- 瀹氫箟澶氫釜鑿滃崟椤?--> <item android:id="@+id/font_10" android:title="@string/font_10"/> <item android:id="@+id/font_12" android:title="@string/font_12"/> <item android:id="@+id/font_14" android:title="@string/font_14"/> <item android:id="@+id/font_16" android:title="@string/font_16"/> <item android:id="@+id/font_18" android:title="@string/font_18"/> </group> </menu> </item> <!-- 瀹氫箟涓?涓?櫘閫氳彍鍗曢」 --> <item android:id="@+id/plain_item" android:title="@string/plain_item"> </item> <item android:title="@string/font_color" android:icon="@drawable/color"> <menu> <!-- 瀹氫箟涓?缁勫厑璁稿?閫夌殑鑿滃崟椤?--> <group> <!-- 瀹氫箟3涓?彍鍗曢」 --> <item android:id="@+id/red_font" android:title="@string/red_title"/> <item android:id="@+id/green_font" android:title="@string/green_title"/> <item android:id="@+id/blue_font" android:title="@string/blue_title"/> </group> </menu> </item></menu>
鑰岃彍鍗曢」琚?崟鍑诲悗鍥炶皟鐨勪竴鑸?啓娉曞?涓嬶細
@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflator = new MenuInflater(this); // 鐘舵?丷.menu.context瀵瑰簲鐨勮彍鍗曪紝骞舵坊鍔犲埌menu涓? inflator.inflate(R.menu.my_menu, menu); return super.onCreateOptionsMenu(menu); }@Override // 閫夐」鑿滃崟鐨勮彍鍗曢」琚?崟鍑诲悗鐨勫洖璋冩柟娉? public boolean onOptionsItemSelected(MenuItem mi) { if(mi.isCheckable()) { mi.setChecked(true); //鈶? } // 鍒ゆ柇鍗曞嚮鐨勬槸鍝?釜鑿滃崟椤癸紝骞堕拡瀵规?х殑浣滃嚭鍝嶅簲銆? switch (mi.getItemId()) { case R.id.font_10: txt.setTextSize(10 * 2); break; case R.id.font_12: txt.setTextSize(12 * 2); break; case R.id.font_14: txt.setTextSize(14 * 2); break; case R.id.font_16: txt.setTextSize(16 * 2); break; case R.id.font_18: txt.setTextSize(18 * 2); break; case R.id.red_font: txt.setTextColor(Color.RED); mi.setChecked(true); break; case R.id.green_font: txt.setTextColor(Color.GREEN); mi.setChecked(true); break; case R.id.blue_font: txt.setTextColor(Color.BLUE); mi.setChecked(true); break; case R.id.plain_item: Toast toast = Toast.makeText(MenuResTest.this, "鎮ㄥ崟鍑讳簡鏅??氳彍鍗曢」", Toast.LENGTH_SHORT); toast.show(); break; } return true; }
浜屻?佷笂涓嬫枃鑿滃崟 ContextMenu
鐢ㄦ埛闀挎寜鐣岄潰鏃朵細鍑虹幇鐨勮彍鍗曢」绉颁负涓婁笅鏂囪彍鍗曘??
鍒涘缓涓婁笅鏂囪彍鍗曠殑璧勬簮鏂囦欢:
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 瀹氫箟涓?缁勫崟閫夎彍鍗曢」鐩?--> <group android:checkableBehavior="single"> <!-- 瀹氫箟3涓?彍鍗曢」 --> <item android:id="@+id/red" android:title="@string/red_title" android:alphabeticShortcut="r"/> <item android:id="@+id/green" android:title="@string/green_title" android:alphabeticShortcut="g"/> <item android:id="@+id/blue" android:title="@string/blue_title" android:alphabeticShortcut="b"/> </group> </menu>
涓烘煇涓?缁勪欢娉ㄥ唽涓婁笅鏂囪彍鍗曚互鍙婅彍鍗曢」琚?崟鍑诲悗鍥炶皟鐨勬柟娉?
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); txt = (TextView) findViewById(R.id.txt); // 涓烘枃鏈??娉ㄥ唽涓婁笅鏂囪彍鍗? registerForContextMenu(txt); }// 鍒涘缓涓婁笅鏂囪彍鍗曟椂瑙﹀彂璇ユ柟娉? @Override public void onCreateContextMenu(ContextMenu menu, View source, ContextMenu.ContextMenuInfo menuInfo) { MenuInflater inflator = new MenuInflater(this); // 鐘舵?丷.menu.context瀵瑰簲鐨勮彍鍗曪紝骞舵坊鍔犲埌menu涓? inflator.inflate(R.menu.context, menu); menu.setHeaderIcon(R.drawable.tools); menu.setHeaderTitle("璇烽?夋嫨鑳屾櫙鑹?); }// 涓婁笅鏂囪彍鍗曚腑鑿滃崟椤硅?鍗曞嚮鏃惰Е鍙戣?鏂规硶銆? @Override public boolean onContextItemSelected(MenuItem mi) { mi.setChecked(true); //鈶? switch (mi.getItemId()) { case R.id.red: mi.setChecked(true); txt.setBackgroundColor(Color.RED); break; case R.id.green: mi.setChecked(true); txt.setBackgroundColor(Color.GREEN); break; case R.id.blue: mi.setChecked(true); txt.setBackgroundColor(Color.BLUE); break; } return true; }
涓夈?丳opupMenu 寮瑰嚭寮忚彍鍗?
浼氬湪鎸囧畾缁勪欢涓婂脊鍑鸿彍鍗曢」锛岄粯璁ゆ儏鍐典笅锛孭opupMenu浼氭樉绀哄湪璇ョ粍浠剁殑涓嬫柟鎴栦笂鏂广??
eg.鍒涘缓PopupMenu骞朵负Popup鑿滃崟鐨勮彍鍗曢」鐨勫崟鍑讳簨浠剁粦瀹氱洃鍚?櫒
// 鍒涘缓PopupMenu瀵硅薄 popup = new PopupMenu(this, button); // 灏哛.menu.popup_menu鑿滃崟璧勬簮鍔犺浇鍒皃opup鑿滃崟涓? getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu()); // 涓簆opup鑿滃崟鐨勮彍鍗曢」鍗曞嚮浜嬩欢缁戝畾浜嬩欢鐩戝惉鍣? popup.setOnMenuItemClickListener( new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()) { case R.id.exit: // 闅愯棌璇ュ?璇濇? popup.dismiss(); break; default: // 浣跨敤Toast鏄剧ず鐢ㄦ埛鐐瑰嚮鐨勮彍鍗曢」 Toast.makeText(PopupMenuTest.this, "鎮ㄥ崟鍑讳簡銆? + item.getTitle() + "銆戣彍鍗曢」" , Toast.LENGTH_SHORT).show(); } return true; } }); popup.show(); }