Android开发手记(12) Menu的使用
Android中的Menu分为三种,分别为:OptionsMenu(选项菜单)、ContextMenu(上下文菜单)、SubMenu(子菜单)。
1、OptionsMenu
按Menu键就会显示,用于当前的Activity。
在Activity里面,一般通过以下函数来使用options menu:
Activity::onCreateOptionsMenu (Menu menu) 创建options menu,这个函数只会在menu第一次显示时调用。
Activity::onPrepareOptionsMenu (Menu menu) 更新改变options menu的内容,这个函数会在menu每次显示时调用。
Activity::onOptionsItemSelected (MenuItem item) 处理选中的菜单项。
1 //menu.add(groupId, itemId, order, title); 通过add()增加menuItem,每个menuItem有groupId,itemId,Order。这三个属性是相互独立,没有关联。 2 MenuItem redMenu = menu.add(MENU_GROUP_1, 0, 0, "Red"); 3 MenuItem OrangedMenu = menu.add(MENU_GROUP_1, 1, 1, "Orange"); 4 MenuItem YellowMenu = menu.add(MENU_GROUP_2, 2, 2, "Yellow"); 5 MenuItem GreenMenu = menu.add(MENU_GROUP_2, 3, 3, "Green"); 6 MenuItem BlueMenu = menu.add(MENU_GROUP_2, 4, 4, "Blue"); 7 MenuItem CyangonMenu = menu.add(MENU_GROUP_3, 5, 5, "Cyangon"); 8 MenuItem PurpleMenu = menu.add(MENU_GROUP_3, 6, 6, "Purple");
1 menu.setGroupCheckable(MENU_GROUP_2, true /*isCheckable*/, false);//第三个参数true为单选,false为多选 2 menu.setGroupEnabled(MENU_GROUP_2, true/*isEnabled*/); 3 menu.setGroupVisible(MENU_GROUP_2, true/*isVisible*/); 4 menu.setGroupCheckable(MENU_GROUP_3, true, true);
完整代码如下:
1 import android.support.v7.app.AppCompatActivity; 2 import android.os.Bundle; 3 import android.view.Menu; 4 import android.view.MenuItem; 5 import android.widget.Toast; 6 7 8 public class MainActivity extends AppCompatActivity { 9 10 private static int MENU_GROUP_1 = 1; 11 private static int MENU_GROUP_2 = 2; 12 private static int MENU_GROUP_3 = 3; 13 14 @Override 15 protected void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.activity_main); 18 } 19 20 @Override 21 public boolean onCreateOptionsMenu(Menu menu) { 22 MenuItem redMenu = menu.add(MENU_GROUP_1, 0, 0, "Red"); 23 MenuItem OrangedMenu = menu.add(MENU_GROUP_1, 1, 1, "Orange"); 24 MenuItem YellowMenu = menu.add(MENU_GROUP_2, 2, 2, "Yellow"); 25 MenuItem GreenMenu = menu.add(MENU_GROUP_2, 3, 3, "Green"); 26 MenuItem BlueMenu = menu.add(MENU_GROUP_2, 4, 4, "Blue"); 27 MenuItem CyangonMenu = menu.add(MENU_GROUP_3, 5, 5, "Cyangon"); 28 MenuItem PurpleMenu = menu.add(MENU_GROUP_3, 6, 6, "Purple"); 29 30 menu.setGroupCheckable(MENU_GROUP_2, true /*isCheckable*/, false);//第三个参数true为单选,false为多选 31 menu.setGroupEnabled(MENU_GROUP_2, true/*isEnabled*/); 32 menu.setGroupVisible(MENU_GROUP_2, true/*isVisible*/); 33 menu.setGroupCheckable(MENU_GROUP_3, true, true); 34 35 return super.onCreateOptionsMenu(menu); 36 } 37 38 @Override 39 public boolean onOptionsItemSelected(MenuItem item) { 40 super.onOptionsItemSelected(item); 41 switch (item.getItemId()) { 42 case 0: 43 Toast.makeText(this, "Red", Toast.LENGTH_SHORT).show(); 44 break; 45 case 1: 46 Toast.makeText(this, "Orange", Toast.LENGTH_SHORT).show(); 47 break; 48 case 2: 49 if(item.isCheckable()){ 50 item.setChecked(!item.isChecked()); 51 } 52 break; 53 case 3: 54 if(item.isCheckable()){ 55 item.setChecked(!item.isChecked()); 56 } 57 break; 58 case 4: 59 if(item.isCheckable()){ 60 item.setChecked(!item.isChecked()); 61 } 62 break; 63 case 5: 64 if(item.isCheckable()){ 65 item.setChecked(true); 66 } 67 break; 68 case 6: 69 if(item.isCheckable()){ 70 item.setChecked(true); 71 } 72 break; 73 } 74 return true; 75 } 76 77 }
2、ContextMenu
要在相应的view上按几秒后才显示的,用于view,跟某个具体的view绑定在一起。这类型的菜单不支持icon和快捷键!
在Activity里面,一般通过以下函数来使用context menu:
Activity::registerForContextMenu(View view) 为某个view注册context menu,一般在Activity::onCreate里面调用。
Activity::onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) 创建context menu,和options menu不同,context meun每次显示时都会调用这个函数。
Activity::onContextItemSelected(MenuItem item) 处理选中的菜单项。
1 menu.setHeaderTitle("ContextMenu"); 2 menu.add(GROUP_COLOR, 0, 0, "Red"); 3 menu.add(GROUP_COLOR, 1, 1, "Green"); 4 menu.add(GROUP_COLOR, 2, 2, "Blue");
菜单响应事件处理和OptionMenu类似。
1 public class MainActivity extends AppCompatActivity { 2 3 private TextView colorText; 4 private static int GROUP_COLOR = 1; 5 6 @Override 7 protected void onCreate(Bundle savedInstanceState) { 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.activity_main); 10 11 colorText = (TextView) findViewById(R.id.textView); 12 registerForContextMenu(colorText); 13 } 14 15 @Override 16 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { 17 super.onCreateContextMenu(menu, v, menuInfo); 18 19 menu.setHeaderTitle("ContextMenu"); 20 menu.add(GROUP_COLOR, 0, 0, "Red"); 21 menu.add(GROUP_COLOR, 1, 1, "Green"); 22 menu.add(GROUP_COLOR, 2, 2, "Blue"); 23 } 24 25 @Override 26 public boolean onContextItemSelected(MenuItem item) { 27 super.onContextItemSelected(item); 28 29 switch (item.getItemId()) { 30 case 0: 31 colorText.setTextColor(Color.RED); 32 break; 33 case 1: 34 colorText.setTextColor(Color.GREEN); 35 break; 36 case 2: 37 colorText.setTextColor( Color.BLUE); 38 break; 39 40 } 41 return true; 42 } 43 44 }
3、SubMenu
SubMenu可以和OptionMenu、ContextMenu配合使用,但子菜单不能嵌套子菜单,这意味着在Android系统,菜单只有两层。同时子菜单不支持icon。
可以在onCreateOptionMenu或者onCreateContextMenu内定义SubMenu:
1 @Override 2 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { 3 super.onCreateContextMenu(menu, v, menuInfo); 4 5 menu.setHeaderTitle("ContextMenu"); 6 menu.add(GROUP_COLOR, 0, 0, "Red"); 7 menu.add(GROUP_COLOR, 1, 1, "Green"); 8 menu.add(GROUP_COLOR, 2, 2, "Blue"); 9 10 SubMenu subMenu = menu.addSubMenu(Menu.NONE, 10, 10, "Text Size"); 11 subMenu.add(Menu.NONE, 11, 11, "Large"); 12 subMenu.add(Menu.NONE, 12, 12, "Small"); 13 }
完整代码:
1 import android.graphics.Color; 2 import android.support.v7.app.AppCompatActivity; 3 import android.os.Bundle; 4 import android.view.ContextMenu; 5 import android.view.Menu; 6 import android.view.MenuItem; 7 import android.view.SubMenu; 8 import android.view.View; 9 import android.widget.TextView; 10 11 public class MainActivity extends AppCompatActivity { 12 13 private TextView colorText; 14 private static int GROUP_COLOR = 1; 15 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity_main); 20 21 colorText = (TextView) findViewById(R.id.textView); 22 registerForContextMenu(colorText); 23 } 24 25 @Override 26 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { 27 super.onCreateContextMenu(menu, v, menuInfo); 28 29 menu.setHeaderTitle("ContextMenu"); 30 menu.add(GROUP_COLOR, 0, 0, "Red"); 31 menu.add(GROUP_COLOR, 1, 1, "Green"); 32 menu.add(GROUP_COLOR, 2, 2, "Blue"); 33 34 SubMenu subMenu = menu.addSubMenu(Menu.NONE, 10, 10, "Text Size"); 35 subMenu.add(Menu.NONE, 11, 11, "Large"); 36 subMenu.add(Menu.NONE, 12, 12, "Small"); 37 } 38 39 @Override 40 public boolean onContextItemSelected(MenuItem item) { 41 super.onContextItemSelected(item); 42 43 switch (item.getItemId()) { 44 case 0: 45 colorText.setTextColor(Color.RED); 46 break; 47 case 1: 48 colorText.setTextColor(Color.GREEN); 49 break; 50 case 2: 51 colorText.setTextColor( Color.BLUE); 52 break; 53 case 11: 54 colorText.setTextSize(30); 55 break; 56 case 12: 57 colorText.setTextSize(20); 58 break; 59 } 60 return true; 61 } 62 63 }
4、XML定义菜单
首先在/res/menu新建一个menu_main.xml,在其中定义相关Menu
1 <menu xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 tools:context=".MainActivity"> 4 <item android:id="@+id/subMenu" 5 android:title="SubMenu" > 6 <menu> <!--定义子菜单--> 7 <item android:id="@+id/subLarge" 8 android:title="Large"/> 9 <item android:id="@+id/subSmall" 10 android:title="Small"/> 11 </menu> 12 </item> 13 <!--定义菜单组1--> 14 <group android:id="@+id/group1" 15 android:checkableBehavior="single" 16 android:enabled="true"> 17 <item android:id="@+id/redMenu" 18 android:title="Red"/> 19 <item android:id="@+id/blueMenu" 20 android:title="Blue"/> 21 </group> 22 <!--定义菜单组2--> 23 <group android:id="@+id/group2" 24 android:checkableBehavior="all" 25 android:enabled="true"> 26 <item android:id="@+id/greenMenu" 27 android:title="Green"/> 28 <item android:id="@+id/purpleMenu" 29 android:title="Purple"/> 30 </group> 31 </menu>
(1)XML定义OptionsMenu
重载onCreateOptionsMenu即可:
1 @Override 2 public boolean onCreateOptionsMenu(Menu menu){ 3 MenuInflater menuInflater = new MenuInflater(getApplication()); 4 menuInflater.inflate(R.menu.menu_main, menu); 5 return super.onCreateOptionsMenu(menu); 6 }
完整代码为:
1 import android.support.v7.app.AppCompatActivity; 2 import android.os.Bundle; 3 import android.view.Menu; 4 import android.view.MenuInflater; 5 import android.widget.TextView; 6 7 public class MainActivity extends AppCompatActivity { 8 9 private TextView colorText; 10 11 @Override 12 protected void onCreate(Bundle savedInstanceState) { 13 super.onCreate(savedInstanceState); 14 setContentView(R.layout.activity_main); 15 } 16 17 @Override 18 public boolean onCreateOptionsMenu(Menu menu){ 19 MenuInflater menuInflater = new MenuInflater(getApplication()); 20 menuInflater.inflate(R.menu.menu_main, menu); 21 return super.onCreateOptionsMenu(menu); 22 } 23 24 }
(2)XML定义ContextMenu
首先为ContextMenu注册菜单项
1 colorText = (TextView)findViewById(R.id.textView); 2 registerForContextMenu(colorText);
然后重载onCreateContextMenu即可:
1 @Override 2 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { 3 super.onCreateContextMenu(menu, v, menuInfo); 4 5 getMenuInflater().inflate(R.menu.menu_main, menu); 6 }
完整代码为:
1 import android.support.v7.app.AppCompatActivity; 2 import android.os.Bundle; 3 import android.view.ContextMenu; 4 import android.view.View; 5 import android.widget.TextView; 6 7 public class MainActivity extends AppCompatActivity { 8 9 private TextView colorText; 10 11 @Override 12 protected void onCreate(Bundle savedInstanceState) { 13 super.onCreate(savedInstanceState); 14 setContentView(R.layout.activity_main); 15 16 colorText = (TextView)findViewById(R.id.textView); 17 registerForContextMenu(colorText); 18 } 19 20 @Override 21 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { 22 super.onCreateContextMenu(menu, v, menuInfo); 23 24 getMenuInflater().inflate(R.menu.menu_main, menu); 25 } 26 27 }