Menu详解(二):利用XML生成菜单和子菜单

前言:上篇,我们说了有关代码生成菜单和子菜单的方法,这里我们再讲讲有关利用XML生成菜单和子菜单的问题。

 

业精于勤,荒于嬉,行成于思,毁于随

(日拱一卒)

 

 

系列文章:

1、《Menu详解(一):代码实现系统菜单及子菜单》

2、《Menu详解(二):利用XML生成菜单和子菜单》

3、《Menu详解(三):使用上下文菜单》

 

一、XML布局

在我们新建工程时,系统会自己为我们生成一个menu文件,在menu文件夹下,名字叫:main.xml;

打开可以看到,里面空空如也,现在我们就加上几项:

 

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.testmenu_xml.MainActivity" >

    <group android:id="@+id/menuGroup_main">
        <item android:id="@+id/menuItem_1"
            android:icon="@drawable/ic_launcher"
            android:title="menu_1"/>
        
         <item android:id="@+id/menuItem_2"
            android:icon="@drawable/ic_launcher"
            android:title="menu_2"/>
         
         <item android:id="@+id/menuItem_3"
            android:icon="@drawable/ic_launcher"
            android:title="menu_3"/>
         
    </group>

</menu>

可以看到完整的Menu布局层次应该是:menu -> group -> item
group标签:对应每一篇中组的概念,id表示组的id值,对应于 menu.add(groupId_1, 1, 1, "menu1_1")的第一个参数;

 

每个标记都有很多的属性,各属性含义如下:

<group>标签:

 

  • id:组的Id值;
  • heckableBehavior:用于指定菜单组内各项菜单项的选择行为,none(不可选)、all(多选 )、single(单选);
  • menuCategory:用于对菜单分类,指定菜单的优先级,可选值为:container、system、secondary和alternative;
  • enabled:用于指定该菜单组中的全部菜单项是否可用;
  • visible:用于指定该菜单组中全部菜单项是否可见;

 

<item>标签:

 

  • id:菜单项的ID;必须唯一
  • title:用于为菜单项指定标题
  • icon:用于为菜单项指定图标
  • enabled:用于指定该菜单项是否可用
  • checkable:用于指定该菜单项是否可选
  • checked:用于指定该菜单项是否已选中
  • visible:用于指定该菜单项是否可见
  • alphabeticShortcut:用于为菜单项指定字符快捷皱键
  • numericShortcut:用于为菜单项指定数字快捷皱键

 

二、使用与响应

XML布局做好了,下一步就是在代码中使用这个布局了。

 

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater menuInflater=getMenuInflater();
        menuInflater.inflate(R.menu.main, menu);
        return true;
    }

使用MenuInflater将main.xml菜单文件与menu绑定起来;

 

下一步是响应菜单项点击,同样,可以在onOptionsItemSelected()通过Id过滤:

 

@Override
public boolean onOptionsItemSelected(MenuItem item) {
	
    int id = item.getItemId();
    //通过ID来响应菜单项
    if (id == R.id.menuItem_1) {
    	Toast.makeText(MainActivity.this, "menu_1", Toast.LENGTH_SHORT).show();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

同样,是通过Item的Id值来判定当前点击的是哪一个菜单项,只是这里匹配的不再是数字,而是R.id.XXX;这里我只对第一个Menu做了响应,当点击时Toast出它的Title,其它没做处理;

 

上面的用例的效果图如下:

(4.0以上出不来Icon ?答案在这篇文章第四节的第三部分(存在问题) :《Menu详解(一):代码实现系统菜单及子菜单》

三、使用子菜单

下面我直接在Item3中添加一个子菜单,给大家看下代码先:

 

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.testmenu_xml.MainActivity" >

    <group android:id="@+id/menuGroup_main">
        <item android:id="@+id/menuItem_1"
            android:icon="@drawable/ic_launcher"
            android:title="menu_1"/>
        
         <item android:id="@+id/menuItem_2"
            android:icon="@drawable/ic_launcher"
            android:title="menu_2"/>
         
         <item android:id="@+id/menuItem_3"
            android:icon="@drawable/ic_launcher"
            android:title="menu_3">
             <menu >
                 <group android:id="@+id/group_2">
                     <item android:id="@+id/sub_1" android:title="sub_1"/>
                     <item android:id="@+id/sub_2" android:title="sub_2"/>
                     <item android:id="@+id/sub_3" android:title="sub_3"/>
                 </group>
             </menu>
         </item>
         
    </group>

</menu>

其实就是在Item3中添加下面代码:

 

 

<menu >
    <group android:id="@+id/group_2">
        <item android:id="@+id/sub_1" android:title="sub_1"/>
        <item android:id="@+id/sub_2" android:title="sub_2"/>
        <item android:id="@+id/sub_3" android:title="sub_3"/>
    </group>
</menu>

可见子菜单也是一个菜单,也包含菜单中的所有部分,menu->group->item,只不过被嵌套在Item里面而已了。添加子菜单后,点击Item3 就会跳进子菜单了,如图:
   


Ok啦,这篇文章比较短,内容也比较少,下篇给大家说说上下文菜单;

 

源码下载地址:http://download.csdn.net/detail/harvic880925/7788711

请大家尊重原创者版权,转载请标明出处:http://blog.csdn.net/harvic880925/article/details/38701351  谢谢!

 

如果你喜欢我的文章,你可能更喜欢我的公众号

启舰杂谈

在HV Menu基础上,改进成读取xml文件的方式来做的菜单,可以横向、纵向,样式可自定义,更加方便了制作动态的菜单和用程序控制菜单权限。XML定义简单直观<br><br><?xml version="1.0" encoding="utf-8" ?><br><br><menus><br> <menu name="文件" link="#" background="" height="20" width="68"><br> <menu name="新建" link="javascript:alert(&apos;新建&apos;)" background="" height="20" width="68"></menu><br> <menu name="打开" link="javascript:alert(&apos;打开&apos;)" background="" height="20" width="68"></menu><br> <menu name="保存" link="javascript:alert(&apos;保存&apos;)" background="" height="20" width="68"></menu><br> <menu name="退出" link="javascript:alert(&apos;退出&apos;)" background="" height="20" width="68"></menu><br> </menu><br> <menu name="编辑" link="#" background="" height="20" width="68"><br> <menu name="复制" link="javascript:alert(&apos;复制&apos;)" background="" height="20" width="68"></menu><br> <menu name="剪切" link="javascript:alert(&apos;剪切&apos;)" background="" height="20" width="68"></menu><br> <menu name="粘贴" link="javascript:alert(&apos;粘贴&apos;)" background="" height="20" width="68"></menu><br> <menu name="删除" link="javascript:alert(&apos;删除&apos;)" background="" height="20" width="68"></menu><br> </menu><br> <menu name="收藏夹" link="" background="" height="20" width="68"><br> <menu name="Google" link="http://www.google.com" background="" height="20" width="68"></menu><br> <menu name="Yahoo" link="http://www.yahoo.com" background="" height="20" width="68"></menu><br> <menu name="邮箱" link="#" background="" height="20" width="68"><br> <menu name="网易163邮箱" link="http://mail.163.com" background="" height="20" width="98"></menu><br> <menu name="网易126邮箱" link="http://mail.126.com" background="" height="20" width="98"></menu><br> <menu name="Gmail" link="http://www.gmail.com" background="" height="20" width="98"></menu><br> </menu><br> </menu><br> <menu name="帮助" link="#" background="" height="20" width="68"><br> <menu name="访问网站" link="http://www.ayeah.net" background="" height="20" width="68"></menu><br> <menu name="发送邮件" link="mailto:ayeah@21cn.com" background="" height="20" width="68"></menu><br> <menu name="论坛" link="javascript:alert(&apos;论坛&apos;)" background="" height="20" width="68"></menu><br> <menu name="关于" link="javascript:alert(&apos;关于&apos;)" background="" height="20" width="68"></menu><br> </menu><br></menus>
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值