菜单之二:使用xml文件定义菜单 分类: H1_ANDROID 2013-11-03 09:39 1038人阅读 评论(0) 收藏
参考《疯狂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) 收藏的更多相关文章
- iOS开源库--最全的整理 分类: ios相关 2015-04-08 09:20 486人阅读 评论(0) 收藏
youtube下载神器:https://github.com/rg3/youtube-dl 我擦咧 vim插件:https://github.com/Valloric/YouCompleteMe vi ...
- iOS 消息推送原理及实现总结 分类: ios技术 2015-03-01 09:22 70人阅读 评论(0) 收藏
在实现消息推送之前先提及几个于推送相关概念,如下图: 1. Provider:就是为指定IOS设备应用程序提供Push的服务器,(如果IOS设备的应用程序是客户端的话,那么Provider可以理解为服 ...
- 服务器证书安装配置指南(IIS7.5) 分类: ASP.NET 2014-11-05 12:39 105人阅读 评论(0) 收藏
1.启动IIS管理器,点击开始菜单->所有程序->管理工具->Internet信息服务(IIS)管理器: 2.选择"服务器证书": 3.在右边窗口,选择" ...
- 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 ...
- 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 ...
- Jquery easy UI 上中下三栏布局 分类: ASP.NET 2015-02-06 09:19 368人阅读 评论(0) 收藏
效果图: 源代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...
- C# IIS应用程序池辅助类 分类: C# Helper 2014-07-19 09:50 249人阅读 评论(0) 收藏
using System.Collections.Generic; using System.DirectoryServices; using System.Linq; using Microsoft ...
- PIGS 分类: POJ 图论 2015-08-10 09:15 3人阅读 评论(0) 收藏
PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18209 Accepted: 8277 Description Mir ...
- Babelfish 分类: 哈希 2015-08-04 09:25 2人阅读 评论(0) 收藏
Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 36398 Accepted: 15554 Descripti ...
随机推荐
- Android 多种方式正确的载入图像,有效避免oom
图像载入的方式: Android开发中消耗内存较多一般都是在图像上面.本文就主要介绍如何正确的展现图像降低对内存的开销,有效的避免oom现象. 首先我们知道我的获取图像的来源一般有三种源 ...
- Android 阅读器架构图,网上收集,留做存货
这个结构图是网上收集的图片.基结构明晰简洁.易于后期维护.本文会继续收集很多其他其他优秀的结构图,望有图的朋友推荐~
- Hadoop权威指南学习笔记三
HDFS简单介绍 声明:本文是本人基于Hadoop权威指南学习的一些个人理解和笔记,仅供学习參考.有什么不到之处还望指出,一起学习一起进步. 转载请注明:http://blog.csdn.net/my ...
- .Net Standard和各平台关系
.NET Standard 1.0 1.1 1.2 1.3 1.4 1.5 1.6 2.0 .NET 核心 1.0 1.0 1.0 1.0 1.0 1 ...
- SPOJ 3899. Finding Fractions 连分数
连分数乱搞,我反正是一眼没看出结果 某巨巨把这题讲解的比较详细 : http://blog.csdn.net/gogdizzy/article/details/8727386 令k = [a/b] 然 ...
- 关于后台接收参数为null的问题之ajax--contentType
ajax方法中的参数: contentType:发送至服务器时内容的编码类型,一般默认:application/x-www-form-urlencoded(适应大多数的场合) dataType:预期服 ...
- HTML基础第四讲---图像
转自:https://blog.csdn.net/likaier/article/details/326735 图像,也就是images,在html语法中用img来表示,其基本的语法是: < ...
- 目标识别(object detection)中的 IoU(Intersection over Union)
首先直观上来看 IoU 的计算公式: 由上述图示可知,IoU 的计算综合考虑了交集和并集,如何使得 IoU 最大,需要满足,更大的重叠区域,更小的不重叠的区域. 两个矩形窗格分别表示: 左上点.右下点 ...
- SN74HC573ANSR 锁存器
这是它的工作电压范围 这个是功能表,数电上曾经学过: 内部的框图,也是数电的知识:
- 最短路算法详解(Dijkstra/SPFA/Floyd)
新的整理版本版的地址见我新博客 http://www.hrwhisper.me/?p=1952 一.Dijkstra Dijkstra单源最短路算法,即计算从起点出发到每个点的最短路.所以Dijkst ...