菜单之二:使用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抓包工具——fiddler抓包分析api接口
本文地址:http://blog.csdn.net/u011733020 首先,写这个仅仅是为了学习.不是要做什么违法的事情,假设有问题,有关部门 请联系我,立刻删除. 不要查我水表. 正题:这里介绍 ...
- AQS -> AbstractQueuedSynchronizer
前言 : 先说说这个 CLH锁: 加锁 1. 创建一个的需要获取锁的 Node 2. 通过 CAS操作 让自己 成为这个尾部的节点,然后令 设置自己的pre 3. 自旋,直到pre节点释放 释放: 1 ...
- 雅黑PHP探针 For PHP7
雅黑PHP探针 For PHP7资料来源: https://kn007.net/topics/yahei-php-probe-for-php7/在v0.4.7版本的基础上,修正了废弃函数及错误语法.使 ...
- php实现希尔排序(总结)
php实现希尔排序(总结) 一.总结 1.希尔排序的算法思路:分组排序, 缩小增量排序,插入排序 2.算法思路: 循环非常好写 有几次gap:log2(n) 每次gap有几组:gap组 每组有几个元素 ...
- 2.1 Producer API官网剖析(博主推荐)
不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ 2.1 Producer API 2.1.生产者API 我们鼓励所有新开发的程序使用 ...
- 至顶网推荐-Rpm另类用法加固Linux安全
http://www.zdnet.com.cn/ 650) this.width=650;" onclick='window.open("http://blog.51cto.com ...
- MySQL 5.7 多实例安装部署实例
1. 背景 MySQL数据库的集中化运维,可以通过在一台服务器上,部署运行多个MySQL服务进程,通过不同的socket监听不同的服务端口来提供各自的服务.各个实例之间是相互独立的,每个实例的dat ...
- 洛谷 P2562 [AHOI2002]Kitty猫基因编码
P2562 [AHOI2002]Kitty猫基因编码 题目描述 小可可选修了基础生物基因学.教授告诉大家 Super Samuel 星球上 Kitty猫的基因的长度都是 2 的正整数次幂 ), 全是由 ...
- [React] Call setState with null to Avoid Triggering an Update in React 16
Sometimes it’s desired to decide within an updater function if an update to re-render should be trig ...
- 文档翻译第003篇:Process Monitor帮助文档(Part 3,附Process Monitor的简单演示)
[导入与导出配置] 一旦您配置了一个筛选器,您能够使用"工具(Tools)"菜单中的"保存筛选器(SaveFilters)"菜单项将其保存.Process Mo ...