参考《疯狂android讲义》2.10节 P174,参见归档project:XmlMenuDemo.zip

一般推荐使用XML文件定义菜单。

基本步骤如下:

1、定义布局文件

为简单显示原理,本布局只有一个EditText

<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=".MainActivity" > <EditText
android:id="@+id/et_hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/hello_world" /> </RelativeLayout>

2、定义菜单资源文件

(1)选项菜单文件

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<!--     注意:string的第一个字母为小写,string.xml文件中也是!!! -->
<item android:title="@string/menu_font_size">
<menu>
<group android:checkableBehavior="single" >
<item
android:id="@+id/font_10"
android:title="@string/font_10"
/>
<item
android:id="@+id/font_20"
android:title="@string/font_20"/>
<item
android:id="@+id/font_30"
android:title="@string/font_30"/>
<item
android:id="@+id/font_40"
android:title="@string/font_40"/>
</group>
</menu>
</item> <item
android:title="@string/plain_menu"
android:id="@+id/menu_plain_menu"
/> </menu>

(2)上下文菜单文件

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 注意:string的第一个字母为小写,string.xml文件中也是!!! --> <group android:checkableBehavior="single" >
<item
android:id="@+id/font_red"
android:alphabeticShortcut="r"
android:title="@string/red"/>
<item
android:id="@+id/font_green"
android:alphabeticShortcut="r"
android:title="@string/green"/>
<item
android:id="@+id/font_blue"
android:alphabeticShortcut="r"
android:title="@string/blue"/>
</group>
</menu>

3、重写onCreateOptionMenu及onCreateContextMenu

4、为组件注册上下文菜单(仅适用于ContextMenu)

5、定义菜单被单击时触发的方法

package com.ljh.xmlmenudemo;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.EditText;
import android.widget.Toast; public class MainActivity extends Activity { private EditText etHelloWorld; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etHelloWorld = (EditText) findViewById(R.id.et_hello_world);
registerForContextMenu(etHelloWorld);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) { getMenuInflater().inflate(R.menu.context, menu);
super.onCreateContextMenu(menu, v, menuInfo);
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// 普通箱单被点击处所进行的操作。
case R.id.menu_plain_menu:
Toast.makeText(this, "你单击了普通菜单", Toast.LENGTH_LONG).show();
break;
// 为子菜单的子项定义被点击时所进行的操作。
case R.id.font_10:
etHelloWorld.setTextSize(10);
break;
case R.id.font_20:
etHelloWorld.setTextSize(20);
break;
case R.id.font_30:
etHelloWorld.setTextSize(30);
break;
case R.id.font_40:
etHelloWorld.setTextSize(40);
break;
}
return super.onOptionsItemSelected(item);
} @Override
public boolean onContextItemSelected(MenuItem item) {
item.setChecked(true);
switch (item.getItemId()) {
case R.id.font_red:
item.setChecked(true);
etHelloWorld.setBackgroundColor(Color.RED);
break;
case R.id.font_green:
item.setChecked(true);
etHelloWorld.setBackgroundColor(Color.GREEN);
break;
case R.id.font_blue:
item.setChecked(true);
etHelloWorld.setBackgroundColor(Color.BLUE);
break;
}
return super.onContextItemSelected(item);
} }

版权声明:本文为博主原创文章,未经博主允许不得转载。

菜单之二:使用xml文件定义菜单 分类: H1_ANDROID 2013-11-03 09:39 1038人阅读 评论(0) 收藏的更多相关文章

  1. iOS开源库--最全的整理 分类: ios相关 2015-04-08 09:20 486人阅读 评论(0) 收藏

    youtube下载神器:https://github.com/rg3/youtube-dl 我擦咧 vim插件:https://github.com/Valloric/YouCompleteMe vi ...

  2. iOS 消息推送原理及实现总结 分类: ios技术 2015-03-01 09:22 70人阅读 评论(0) 收藏

    在实现消息推送之前先提及几个于推送相关概念,如下图: 1. Provider:就是为指定IOS设备应用程序提供Push的服务器,(如果IOS设备的应用程序是客户端的话,那么Provider可以理解为服 ...

  3. 服务器证书安装配置指南(IIS7.5) 分类: ASP.NET 2014-11-05 12:39 105人阅读 评论(0) 收藏

    1.启动IIS管理器,点击开始菜单->所有程序->管理工具->Internet信息服务(IIS)管理器: 2.选择"服务器证书": 3.在右边窗口,选择" ...

  4. Hdu 1507 Uncle Tom's Inherited Land* 分类: Brush Mode 2014-07-30 09:28 112人阅读 评论(0) 收藏

    Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  5. Poj 1050 分类: Translation Mode 2014-04-04 09:31 103人阅读 评论(0) 收藏

    To the Max Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39058   Accepted: 20629 Desc ...

  6. Jquery easy UI 上中下三栏布局 分类: ASP.NET 2015-02-06 09:19 368人阅读 评论(0) 收藏

    效果图: 源代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...

  7. C# IIS应用程序池辅助类 分类: C# Helper 2014-07-19 09:50 249人阅读 评论(0) 收藏

    using System.Collections.Generic; using System.DirectoryServices; using System.Linq; using Microsoft ...

  8. PIGS 分类: POJ 图论 2015-08-10 09:15 3人阅读 评论(0) 收藏

    PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18209 Accepted: 8277 Description Mir ...

  9. Babelfish 分类: 哈希 2015-08-04 09:25 2人阅读 评论(0) 收藏

    Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 36398 Accepted: 15554 Descripti ...

随机推荐

  1. CCNP路由实验之十五 NAT(网络地址转换)

     CCNP路由实验之十五 NAT(网络地址转换) 众所周知,要让自己的电脑连上Internet,必须要到运营商(ISP)申请一个上网账号,依据此账号申请自己的宽频业务(拨号上网.商业固定IP等等) ...

  2. go每个函数写代码例子

    https://github.com/astaxie/gopkg 由于目前golang的手册里面针对函数的例子太少了,很多时候不知道怎么使用,好多人都是看源代码才明白怎么用,这个给我们快速开发gola ...

  3. BZOJ1014[JSOI2008]火星人prefix(splay维护hash)

    Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...

  4. 1.字符设备驱动------Linux中断处理体系结构

    一.中断处理体系结构的初始化 Linux内核将所有的中断统一编号,使用一个irq_desc结构数组来描述这些中断;每个数组项对应一个中断,也可能是一组中断,它们共用相同的中断号,里面记录了中断的名称. ...

  5. django遇到的那些古怪问题

    AssertionError: .accepted_renderer not set on Response 出错原因,没有在合法的方法内使用 response 响应,之前在dispatch内直接re ...

  6. 2018/8/21 qbxt测试

    2018/8/21 qbxt测试 期望得分:0? 实际得分:0 思路:manacher   会写模板但是不会用 qwq 听了某人的鬼话,直接输出0,然后就gg了 #include <cstdio ...

  7. oracle数据库spfile

    http://blog.chinaunix.net/uid-8996530-id-3195808.html http://www.cnblogs.com/HondaHsu/p/4885318.html ...

  8. 洛谷 P1068 分数线划定

    P1068 分数线划定 题目描述 世博会志愿者的选拔工作正在 A 市如火如荼的进行.为了选拔最合适的人才,A 市对 所有报名的选手进行了笔试,笔试分数达到面试分数线的选手方可进入面试.面试分数线根 据 ...

  9. FFmpegh.264解码

    - (int)DecodeH264Frames: (unsigned char*)inputBuffer withLength:(int)aLength { ; ; av_init_packet(&a ...

  10. 基于ContentObserver来动态取消或加入屏幕超时任务

    前面也说了.ContentObserver能够来监控数据库里某一项数据的变化,当然也能够同一时候监控多个数据项的变化. 笔者在项目中须要改动到屏幕超时的需求,比方在车载业务中,倒车事件发生的时候,是不 ...