单 词 | 说 明 |
OptionMenu | 普通菜单 |
SubMenu | 子菜单 |
ContextMenu | 上下文菜单 |
手机的屏幕比较小,不可能所有的功能都放在activity上展示,那么那些常用的功能就可以放置在菜单里面,操作比较方便,用户体验好。
普通菜单:按menu键弹出菜单
子菜单:单击子菜单弹出子菜单
上下文菜单:长按单条记录弹出上下文菜单
添加普通菜单步骤
方式一:代码创建
①在Activity中实现onCreateOptionMenu方法
②menu.add(groupId, itemId, order, title);
参数1:分组的id
参数2:子项的id
参数3:排序的id (跟分组无关)
参数4:按钮的显示名称
③添加点击事件
实现onOptionsItemSelected(MenuItem item)方法
用switch判断是哪个按钮被点击了。
switch(item.getItemId()){
case 0:
Toast.makeText(this, "新建成功", Toast.LENGTH_LONG).show();
break;
case 1:
Toast.makeText(this, "删除成功", Toast.LENGTH_LONG).show();
break;
default:
break;
}
方式二:布局文件创建
①在res/menu/main.xml
在<menu></menu>中添加<item/>
<item
android:id="@+id/new_create"//id
android:orderInCategory="100"//排序
android:title="新建"/>//名字
②在(Activity)代码中引用(菜单布局加载器:getMenuInflater().inflate(创建菜单的布局文件,要引用子菜单的主菜单));
switch(item.getItemId()){
case R.id.new_create_menu:
Toast.makeText(MainActivity.this, "新建成功..", Toast.LENGTH_LONG).show();
break;
case R.id.new_create_delete:
Toast.makeText(MainActivity.this, "删除成功..", Toast.LENGTH_LONG).show();
break;
case R.id.new_create_menu1:
Toast.makeText(MainActivity.this, "新建大学生..", Toast.LENGTH_LONG).show();
break;
case R.id.new_create_menu2:
Toast.makeText(MainActivity.this, "新建小学生..", Toast.LENGTH_LONG).show();
break;
default:
break;
}
在新建中创建子菜单
在res/menu/main.xml
在<menu></menu>中添加<menu></menu>
案例
MainActivity.java
public class MainActivity extends Activity {
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>protected void onCreate(Bundle savedInstanceState) {
<span style="white-space:pre"> </span>super.onCreate(savedInstanceState);
<span style="white-space:pre"> </span>setContentView(R.layout.main);
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public boolean onCreateOptionsMenu(Menu menu) {
//------------------------用代码创建---------------------
//<span style="white-space:pre"> </span>menu.add(0, 0, 0, "新建");
//<span style="white-space:pre"> </span>menu.add(1, 1, 1, "删除");
<span style="white-space:pre"> </span>
//------------------------用布局文件---------------------
<span style="white-space:pre"> </span>getMenuInflater().inflate(R.menu.main, menu);
<span style="white-space:pre"> </span>return super.onCreateOptionsMenu(menu);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public boolean onOptionsItemSelected(MenuItem item) {
//------------------------用代码创建---------------------
//<span style="white-space:pre"> </span>switch(item.getItemId()){
//<span style="white-space:pre"> </span>case 0:
//<span style="white-space:pre"> </span>Toast.makeText(this, "新建成功", Toast.LENGTH_LONG).show();
//<span style="white-space:pre"> </span>break;
//<span style="white-space:pre"> </span>case 1:
//<span style="white-space:pre"> </span>Toast.makeText(this, "删除成功", Toast.LENGTH_LONG).show();
//<span style="white-space:pre"> </span>break;
//<span style="white-space:pre"> </span>default:
//<span style="white-space:pre"> </span>break;
//<span style="white-space:pre"> </span>}
//------------------------用布局文件---------------------
<span style="white-space:pre"> </span>switch(item.getItemId()){
<span style="white-space:pre"> </span>case R.id.new_create:
<span style="white-space:pre"> </span>Toast.makeText(this, "新建成功", Toast.LENGTH_LONG).show();
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case R.id.delete_menu:
<span style="white-space:pre"> </span>Toast.makeText(this, "删除成功", Toast.LENGTH_LONG).show();
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case R.id.new_create_x:
<span style="white-space:pre"> </span>Toast.makeText(this, "新建X成功", Toast.LENGTH_LONG).show();
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case R.id.new_create_y:
<span style="white-space:pre"> </span>Toast.makeText(this, "新建Y成功", Toast.LENGTH_LONG).show();
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>default:
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>return super.onOptionsItemSelected(item);
<span style="white-space:pre"> </span>}
}
res/menu/main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/new_create"
android:orderInCategory="100"
android:title="新建">
<menu>
<item
android:id="@+id/new_create_x"
android:orderInCategory="78"
android:title="新建x"/>
<span style="white-space:pre"> </span> <item
<span style="white-space:pre"> </span> android:id="@+id/new_create_y"
<span style="white-space:pre"> </span> android:orderInCategory="101"
<span style="white-space:pre"> </span> android:title="新建y"/>
</menu>
</item>
<item
android:id="@+id/delete_menu"
android:orderInCategory="77"
android:title="删除"/>
</menu>
Tips:menu一定要包含在父item中