菜单之二:使用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 ...
随机推荐
- Trafodion:Transactional SQL on HBase
Trafodion: Transactional SQL on HBase HBase上实时分布式事务处理 介绍 HBase的SQL能力一直不足.Phoenix缺乏Join能力,eBay提出的kyli ...
- ByteUtils
package sort.bing.com; import java.io.ByteArrayOutputStream;import java.io.DataOutputStream;import j ...
- CF1009F Dominant Indices(树上DSU/长链剖分)
题目大意: 就是给你一棵以1为根的树,询问每一个节点的子树内节点数最多的深度(相对于这个子树根而言)若有多解,输出最小的. 解题思路: 这道题用树链剖分,两种思路: 1.树上DSU 首先想一下最暴力的 ...
- R语言-有负下标里才干有零
1.仅仅有负下标里才干有零 先看一个样例 >a<-c(1,2,3,4) >a[-1:1] > a[-1:1] Error in a[-1:1] : 仅仅有负下标里才干有零 (1 ...
- android adb常见问题的解决方法!
** adb的常见问题 adb:android debug bridge,用于连接模拟器/手机与PC端软件(比如:eclipse或者xx手机助手) adb devices -> ...
- js面向对象2--原型
一.原型和原型对象 函数的原型prototype:函数才有prototype,prototype是一个对象,指向了当前构造函数的引用地址. 所有对象都有__proto__属性, 所有的__proto ...
- Django环境搭建(一)
搭建Django环境之前先搭建python运行环境 需要了解: 解释器(编译器): 计算机不能直接理解任何除机器语言外的其他语言,所以程序员必须要把自己写的语言翻译成机器语言,而将其他语言翻译成机器语 ...
- QQ群功能设计与心理学
刚刚在一个Java技术交流群,发了个 "博客投票"的广告. 群主两眼一黑,瞬间就把我给干掉了. 看到QQ给出的系统消息,发现QQ群的一个功能做得很不错. 大家注意到,右边有个&qu ...
- programming+windows+MFC
1)CMyApp declares no data members 2)CWinApp::InitInstance run after application build but before the ...
- 【例题 6-20 UVA - 1599】Ideal Path
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 逆向做一遍bfs. 得到终点到某个点的最短距离. 这样,我们从起点顺序的时候. 就能知道最短路的下一步是要走哪里了. 这样,我们从起 ...