官方文档:menus:http://androiddoc.qiniudn.com/guide/topics/ui/menus.html
1.OptionMenu(选项菜单) 1)如何使用OptionMenu? 答:非常简单,重写两个方法就好,其实这两个方法我们在创建项目的时候就会自动生成~ 他们分别是: public boolean onCreateOptionsMenu(Menu menu):调用OptionMenu,在这里完成菜单初始化 public boolean onOptionsItemSelected(MenuItem item):菜单项被选中时触发,这里完成事件处理 当然除了上面这两个方法我们可以重写外我们还可以重写这三个方法: public void onOptionsMenuClosed(Menu menu):菜单关闭会调用该方法 public boolean onPrepareOptionsMenu(Menu menu):选项菜单显示前会调用该方法, 可在这里进行菜单的调整(动态加载菜单列表) public boolean onMenuOpened(int featureId, Menu menu):选项菜单打开以后会调用这个方法 而加载菜单的方式有两种,一种是直接通过编写菜单XML文件,然后调用: getMenuInflater().inflate(R.menu.menu_main, menu);加载菜单 或者通过代码动态添加,onCreateOptionsMenu的参数menu,调用add方法添加 菜单,add(菜单项的组号,ID,排序号,标题),另外如果排序号是按添加顺序排序的话都填0即可
代码实现: MainActivity.java: public class MainActivity extends AppCompatActivity { //1.定义不同颜色的菜单项的标识: final private int RED = 110; final private int GREEN = 111; final private int BLUE = 112; final private int YELLOW = 113; final private int GRAY= 114; final private int CYAN= 115; final private int BLACK= 116; private TextView tv_test; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_test = (TextView) findViewById(R.id.tv_test); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. menu.add(1,RED,4,"红色"); menu.add(1,GREEN,2,"绿色"); menu.add(1,BLUE,3,"蓝色"); menu.add(1,YELLOW,1,"黄色"); menu.add(1,GRAY,5,"灰色"); menu.add(1,CYAN,6,"蓝绿色"); menu.add(1,BLACK,7,"黑色"); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); switch (id){ case RED: tv_test.setTextColor(Color.RED); break; case GREEN: tv_test.setTextColor(Color.GREEN); break; case BLUE: tv_test.setTextColor(Color.BLUE); break; case YELLOW: tv_test.setTextColor(Color.YELLOW); break; case GRAY: tv_test.setTextColor(Color.GRAY); break; case CYAN: tv_test.setTextColor(Color.CYAN); break; case BLACK: tv_test.setTextColor(Color.BLACK); break; } return super.onOptionsItemSelected(item); } }
另外通过XML方式定义Menu的方式,贴个简单的参考代码: <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/new_game" android:icon="@drawable/ic_new_game" android:title="@string/new_game" android:showAsAction="ifRoom"/> <item android:id="@+id/help" android:icon="@drawable/ic_help" android:title="@string/help" /> </menu>
1)如何使用ContextMenu? 答:使用的流程如下: Step 1:重写onCreateContextMenu()方法 Step 2:为view组件注册上下文菜单,使用registerForContextMenu()方法,参数是View Step 3:重写onContextItemSelected()方法为菜单项指定事件监听器
实现代码: 我们先来编写选项菜单的菜单XML文件: menu_context.xml: <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 定义一组单选按钮 --> <!-- checkableBehavior的可选值由三个:single设置为单选,all为多选,none为普通选项 --> <group android:checkableBehavior="none"> <item android:id="@+id/blue" android:title="@string/font_blue"/> <item android:id="@+id/green" android:title="@string/font_green"/> <item android:id="@+id/red" android:title="@string/font_red"/> </group> </menu> 接着我们在选项菜单的那个基础上,添加一个TextView,然后加上下面一些东西: private TextView tv_context; tv_context = (TextView) findViewById(R.id.tv_context); registerForContextMenu(tv_context); //重写与ContextMenu相关方法 @Override //重写上下文菜单的创建方法 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { MenuInflater inflator = new MenuInflater(this); inflator.inflate(R.menu.menu_context, menu); super.onCreateContextMenu(menu, v, menuInfo); } //上下文菜单被点击是触发该方法 @Override public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.blue: tv_context.setTextColor(Color.BLUE); break; case R.id.green: tv_context.setTextColor(Color.GREEN); break; case R.id.red: tv_context.setTextColor(Color.RED); break; } return true; }
3.SubMenu(子菜单) 所谓的子菜单只是在<**item**>中又嵌套了一层<**menu**>而已
实现代码: 编写子菜单的Menu文件:menu_sub.xml: <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/submenu" android:title="子菜单使用演示~"> <menu> <group android:checkableBehavior = "none"> <item android:id="@+id/one" android:title = "子菜单一"/> <item android:id="@+id/two" android:title = "子菜单二"/> <item android:id="@+id/three" android:title = "子菜单三"/> </group> </menu> </item> </menu> 接着我们改下上面上下文菜单的两个方法的内容,换上下面的代码: public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { //子菜单部分: MenuInflater inflator = new MenuInflater(this); inflator.inflate(R.menu.menu_sub, menu); super.onCreateContextMenu(menu, v, menuInfo); } public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.one: Toast.makeText(MainActivity.this,"你点击了子菜单一",Toast.LENGTH_SHORT).show(); break; case R.id.two: item.setCheckable(true); Toast.makeText(MainActivity.this,"你点击了子菜单二",Toast.LENGTH_SHORT).show(); break; case R.id.three: Toast.makeText(MainActivity.this,"你点击了子菜单三",Toast.LENGTH_SHORT).show(); item.setCheckable(true); break; } return true; }
4.PopupMenu(弹出式菜单) 一个类似于PopupWindow的东东,他可以很方便的在指定View下显示一个弹出菜单,而且 他的菜单选项可以来自于Menu资源
实现代码: 菜单资源文件:menu_pop.xml: <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/lpig" android:title="小猪" /> <item android:id="@+id/bpig" android:title="大猪" /> </menu> 在布局中添加一个按钮,然后添加点击事件: MainActivity.java: btn_show_menu.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { PopupMenu popup = new PopupMenu(MainActivity.this,btn_show_menu); popup.getMenuInflater().inflate(R.menu.menu_pop, popup.getMenu()); popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()){ case R.id.lpig: Toast.makeText(MainActivity.this,"你点了小猪~", Toast.LENGTH_SHORT).show(); break; case R.id.bpig: Toast.makeText(MainActivity.this,"你点了大猪~", Toast.LENGTH_SHORT).show(); break; } return true; } }); popup.show(); } });