含快捷键的菜单 <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/invisible_item" android:visible="false" android:alphabeticShortcut="i" android:title="Invisible item" /> <item android:id="@+id/a_item" android:alphabeticShortcut="a" //a为快捷键(menu+a) android:title="Alvin" /> <item android:id="@+id/b_item" android:alphabeticShortcut="b" android:title="Bart" /> <item android:id="@+id/c_item" android:alphabeticShortcut="c" android:title="Chris" /> <item android:id="@+id/d_item" android:alphabeticShortcut="d" android:title="David" /> <item android:id="@+id/e_item" android:alphabeticShortcut="e" android:title="Eric" /> <item android:id="@+id/f_item" android:alphabeticShortcut="f" android:title="Frank" /> <item android:id="@+id/g_item" android:alphabeticShortcut="g" android:title="Gary" /> <item android:id="@+id/h_item" android:alphabeticShortcut="h" android:title="Henry" /> <item android:id="@+id/excl_item" android:alphabeticShortcut="!" android:title="Exclamation" /> </menu>
含非选择项菜单<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/enabled_item"android:title="Enabled"android:icon="@drawable/stat_happy" /> <item android:id="@+id/disabled_item"android:title="Disabled"android:enabled="false"android:icon="@drawable/stat_sad" /> <item android:id="@+id/enabled_item_2"android:title="Enabled"android:icon="@drawable/stat_happy" /> <item android:id="@+id/disabled_item_2"android:title="Disabled"android:enabled="false"android:icon="@drawable/stat_sad" /> <item android:id="@+id/enabled_item_3"android:title="Enabled"android:icon="@drawable/stat_happy" /> <item android:id="@+id/disabled_item_3"android:title="Disabled"android:enabled="false"android:icon="@drawable/stat_sad" /> <item android:id="@+id/enabled_item_4"android:title="Enabled"android:icon="@drawable/stat_happy" /> <item android:id="@+id/disabled_item_4"android:title="Disabled"android:enabled="false"android:icon="@drawable/stat_sad" /> </menu>
Android的菜单既可以用代码添加可以用xml文件构造,下面给大家介绍分别介绍下一、用Xml文件构造
Java代码:
/构建函数 public boolean onCreateOptionsMenu(Menu menu) { // Hold on to this mMenu = menu; // Inflate the currently selected menu XML resource. MenuInflater inflater = getMenuInflater(); inflater.inflate(sMenuExampleResources[mSpinner.getSelectedItemPosition()], menu); } //回调函数 public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { // For "Title only": Examples of matching an ID with one assigned in // the XML case R.id.jump: Toast.makeText(this, "Jump up in the air!", Toast.LENGTH_SHORT).show(); return true; case R.id.dive: Toast.makeText(this, "Dive into the water!", Toast.LENGTH_SHORT).show(); return true; nu); }
含图片和文字的菜单
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/happy" android:title="Happy" android:icon="@drawable/stat_happy" /> <item android:id="@+id/neutral" android:title="Neutral" android:icon="@drawable/stat_neutral" /> <item android:id="@+id/sad" android:title="Sad" android:icon="@drawable/stat_sad" /> </menu>