参考《疯狂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. Extjs, 使用GridPanel出现 Layout run failed

    当GridPanel被加入到容器,且容器的layout为vbox时候, 会出现 Layout run failed 后者GridPanel的尺寸没有撑满父容器 网上找到的解决的方法是.要给父容器设置一 ...

  2. 使用MERGE语句同步表

    先建好測试环境: USE TEMPDB GO IF OBJECT_ID('T1') IS NOT NULL DROP TABLE T1 IF OBJECT_ID('T2') IS NOT NULL D ...

  3. [NOI.AC#34]palinedrome 字符串hash+贪心

    容易看出,只要从两边往中间扫描,碰到相等的就直接分割然后加入答案即可,判断相等用字符串hash #include<bits/stdc++.h> #define REP(i,a,b) for ...

  4. 36.intellij idea 如何一键清除所有断点

    转自:https://www.cnblogs.com/austinspark-jessylu/p/7799212.html 1.在idea左下方找到"View Breakpoints&quo ...

  5. 用djbdns为域名解析服务护航

      上期回顾:http://chenguang.blog.51cto.com/350944/292195       650) this.width=650;" alt="&quo ...

  6. SpringMVC 传递相同名称的参数的最佳方法

    华为云4核8G,高性能云服务器,免费试用 >>>   SpringMVC 多个对象的相同字段参数传递解决方案,在SpringMVC中,有时需要传递多个对象(除了Model和web元素 ...

  7. [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 ...

  8. menu-普通menu弹出框样式

    今天接触到了menu弹出框样式.主要就是在theme下进行调整.现在把接触到的知识点总结一下. 在theme中,跟menu有关的几个属性如下 <item name="panelBack ...

  9. 10. ZooKeeper之搭建伪集群模式。

    转自:https://blog.csdn.net/en_joker/article/details/78673456 在集群和单机两种模式下,我们基本完成了分别针对生产环境和开发环境ZooKeeper ...

  10. 2. ZooKeeper的ZAB协议。

    转自:https://blog.csdn.net/en_joker/article/details/78662880 ZooKeeper并没有完全采用Paxos算法,而是使用了一种称为ZooKeepe ...