Menu(选项菜单,上下文菜单,弹出式菜单)
2016-06-26 12:50
405 查看
选择菜单,两种方式:一个是手动创建添加,另一个时通过布局填充器添加
上下文菜单,需要注册上下文菜单
弹出式菜单
Menu文件下的布局,可以添加子菜单
public class MyMenu extends Activity { //Menu会显示在左上角 @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); } // 用于创建菜单选项菜单,在打开界面时会自动调用 @Override public boolean onCreateOptionsMenu(Menu menu) { //手动创建 // 组的ID,当前选项的ID,排序,标题名称 /*menu.add(0, 100, 1, "导航"); menu.add(0, 200, 2, "功能"); menu.add(0, 300, 3, "设置");*/ //菜单填充器 getMenuInflater().inflate(R.menu.main, menu); return super.onCreateOptionsMenu(menu); } // 通过item来确定选项菜单的单击时间 @Override public boolean onOptionsItemSelected(MenuItem item) { int itemId = item.getItemId(); //手动创建 /*switch (itemId) { case 100: Toast.makeText(this, "点击的是导航", Toast.LENGTH_LONG).show(); break; case 200: Toast.makeText(this, "功能", Toast.LENGTH_LONG).show(); break; case 300: Toast.makeText(this, "设置", Toast.LENGTH_LONG).show(); break; }*/ //填充器 switch (itemId) { case R.id.one: Toast.makeText(this, "点击的是导航", Toast.LENGTH_LONG).show(); break; case R.id.tow: Toast.makeText(this, "功能", Toast.LENGTH_LONG).show(); break; case R.id.there: Toast.makeText(this, "设置", Toast.LENGTH_LONG).show(); break; } return super.onOptionsItemSelected(item); } }
上下文菜单,需要注册上下文菜单
public class MycontectMenuActivity extends Activity { private TextView tv; //上下文菜单 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_mycontect_menu); tv=(TextView) findViewById(R.id.ontouch); //注册上下文菜单到组件上 基于长按点击事件 registerForContextMenu(tv); } //创建上下文菜单的事件方法 @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); //menu.add(arg0); getMenuInflater().inflate(R.menu.mycontect_menu, menu); } //上下文单击时间 @Override public boolean onContextItemSelected(MenuItem item) { int itemId = item.getItemId(); switch (itemId) { case R.id.red: tv.setBackgroundColor(Color.RED); break; case R.id.bolue: tv.setBackgroundColor(Color.BLUE); break; case R.id.green: tv.setBackgroundColor(Color.GREEN); break; } return super.onContextItemSelected(item); } }
弹出式菜单
public class MycontectMenuActivity extends Activity { private TextView tv; //上下文菜单 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_mycontect_menu); tv=(TextView) findViewById(R.id.ontouch); //注册上下文菜单到组件上 基于长按点击事件 registerForContextMenu(tv); } //创建上下文菜单的事件方法 @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); //menu.add(arg0); getMenuInflater().inflate(R.menu.mycontect_menu, menu); } //上下文单击时间 @Override public boolean onContextItemSelected(MenuItem item) { int itemId = item.getItemId(); switch (itemId) { case R.id.red: tv.setBackgroundColor(Color.RED); break; case R.id.bolue: tv.setBackgroundColor(Color.BLUE); break; case R.id.green: tv.setBackgroundColor(Color.GREEN); break; } return super.onContextItemSelected(item); } }
Menu文件下的布局,可以添加子菜单
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <!-- android:showAsAction="never" 不显示 android:showAsAction="always" 一直显示 android:showAsAction="ifRoom" 有空间了显示--> <item android:id="@+id/s" android:orderInCategory="100" android:showAsAction="never" android:title="小号"/> <item android:id="@+id/xl" android:orderInCategory="100" android:showAsAction="never" android:title="中号"/> <item android:id="@+id/xxl" android:orderInCategory="100" android:showAsAction="never" android:title="大号"/> <item android:id="@+id/xxxl" android:orderInCategory="100" android:showAsAction="never" android:title="特号"> <menu> <item android:id="@+id/xxxxl" android:orderInCategory="100" android:showAsAction="never" android:title="特二"/> </menu> </item> </menu>
相关文章推荐
- 升级Mac内置的vim
- 考研英语Android版演示文档
- Git终端命令详解
- http://www.tuicool.com/articles/NBBnum
- http://www.tuicool.com/articles/NBBnum
- 张尔摩斯集
- Android报No resource found that matches the given name问题的解决
- Spark算子:RDD键值转换操作(1)–partitionBy、mapValues、flatMapValues
- Linux 程序运行栈帧
- 对于常用的端口号
- Android自学笔记(一)Android系统架构
- Chain Of Responsibility(职责连)-对象行为型模式
- linux0.11版内核的mktime分析
- Palisade.DecisionTools.Suite.7.0.Win
- 嘻哈软件开发---设计
- 最大流 — Edmond Karp算法
- 打印机服务器搭建 -cups
- Spark算子:RDD基本转换操作(6)–zip、zipPartitions
- 正态分布R语言实例
- HBase的RPC源码分析