menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 嵌套菜单
1.在item中添加menu标签,menu标签中再添加item标签
2.二级菜单以对话框形式出现,最多两级菜单-->
<item
android:id="@+id/menu_1"
android:title="早餐"/>
<item
android:id="@+id/menu_2"
android:title="午餐"/>
<item
android:id="@+id/menu_3"
android:title="晚餐">
<menu>
<item
android:id="@+id/menu3_1"
android:title="鱼香肉丝"/>
<item
android:id="@+id/menu3_2"
android:title="香酥鸡块"/>
<item
android:id="@+id/menu3_3"
android:title="皮蛋瘦肉粥"/>
</menu>
</item>
</menu>

xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.menudemo.SubMenuDemo" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> </RelativeLayout>

源代码:

package com.example.menudemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.widget.Toast; public class SubMenuDemo extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_submenu);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
/**用xml文件加载菜单栏
* getMenuInflater().inflate(R.menu.sub_menu_demo, menu);
*/
        /**通过代码的方式添加菜单
* 通过代码添加带有子菜单的菜单
*/
        SubMenu file = menu.addSubMenu("文件");
file.add(1, 1, 1, "新建");
file.add(1, 2, 1, "打开");
file.add(1, 3, 1, "保存");
file.setHeaderIcon(R.drawable.ic_launcher);
file.setHeaderTitle("文件操作"); SubMenu edit = menu.addSubMenu("编辑");
edit.add(2,1,1,"复制");
edit.add(2,2,1,"粘贴");
edit.add(2,3,1,"剪切");
edit.setHeaderIcon(R.drawable.ic_launcher);
edit.setHeaderTitle("编辑操作");
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.
switch(item.getGroupId()){
case 1:{
if(item.getItemId()==1){
Toast.makeText(this,"点击了新建" , Toast.LENGTH_SHORT).show();
}else if(item.getItemId()==2){
Toast.makeText(this,"点击了打开" , Toast.LENGTH_SHORT).show();
}if(item.getItemId()==3){
Toast.makeText(this,"点击了保存" , Toast.LENGTH_SHORT).show();
}
break;
}case 2:
if(item.getItemId()==1){
Toast.makeText(this,"点击了复制" , Toast.LENGTH_SHORT).show();
}else if(item.getItemId()==2){
Toast.makeText(this,"点击了粘贴" , Toast.LENGTH_SHORT).show();
}if(item.getItemId()==3){
Toast.makeText(this,"点击了剪切" , Toast.LENGTH_SHORT).show();
}
}
return super.onOptionsItemSelected(item);
}
}

Android_menu_SubMenu的更多相关文章

随机推荐

  1. 学习xcode 博客

    csdn http://blog.csdn.net/tianyitianyi1/article/category/1160169/2

  2. POJ2104 K-th Number 划分树 模板题啊

    /*Source Code Problem: 2104 User: 96655 Memory: 14808K Time: 1282MS Language: G++ Result: Accepted S ...

  3. Python脚本控制的WebDriver 常用操作 <十八> 获取测试对象的css属性

    测试用例场景 当你的测试用例纠结细枝末节的时候,你就需要通过判断元素的css属性来验证你的操作是否达到了预期的效果.比如你可以通过判断页面上的标题字号以字体来验证页面的显示是否符合预期.当然,这个是强 ...

  4. 【原】Redis事务管理

    Redis高级篇 事务 MULTI, EXEC, DISCARD and WATCH命令用于保证Redis中的事务处理 一个事务中的所有命令被序列化并串行执行. 事务的原子性. 用法 MULTI ...

  5. Js 与 TextArea

    当给一个js变量赋值一个有换行的值得时候,就会报错: <!DOCTYPE HTML> <html> <head> <script src="http ...

  6. leetcode—Palindrome 解题报告

    1.题目描述 Given a string s, partition s such that every substring of the partition is a palindrome. Ret ...

  7. 基于adt-bundle的Android开发环境搭建

    web与移动是当今的热门,怎么说都得会一点,完全不懂是不行的. 一直想玩一下移动开发,穷屌丝暂时没有iPhone和Mac,所以先拿Android开刀. 之前也有想过玩一下Android,但是都被各种博 ...

  8. hadoop源码导入eclipse

    1,下载hadoop源码 下载链接 http://svn.apache.org/repos/asf/hadoop/common/tags/release-2.2.0/   为2.2.0的源码, 也可以 ...

  9. HW6.24

    public class Solution { public static void main(String[] args) { int count = 0; int color; int numbe ...

  10. .net iis 域名泛解析实战

    最近做个人网站想实现多个二级域名,一来为了好记,二来为了搜索引擎优化,搜索引擎对二级域名的收录还是比较快的.刚开始做了4,5个二级域名,每个都是在域名解析后台手动添加的,不过随着二级域名越来越多,发现 ...