菜单之二:使用xml文件定义菜单
来源:互联网 发布:五年级小数简便算法 编辑:程序博客网 时间:2024/05/21 05:42
参考《疯狂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;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);etHelloWorld = (EditText) findViewById(R.id.et_hello_world);registerForContextMenu(etHelloWorld);}@Overridepublic 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;}@Overridepublic void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {getMenuInflater().inflate(R.menu.context, menu);super.onCreateContextMenu(menu, v, menuInfo);}@Overridepublic 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);}@Overridepublic 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文件定义菜单
- 菜单之二:使用xml文件定义菜单
- 定义XML菜单文件
- 通过xml文件定义菜单
- 使用 XML 定义选项菜单
- 使用菜单XML文件创建菜单
- sdk之菜单使用二
- 第35章:使用xml文件定义菜单(Menu)并设置监听事件
- 在XML中定义菜单
- 在XML中定义菜单
- 使用xml布局菜单
- 使用XML生成菜单
- 使用XML生成菜单
- 使用XML生成菜单
- 使用xml菜单资源
- 微信之定义菜单
- 使用dom4j从XML配置文件中生成菜单(二)
- Android 之MenuInflater的使用(布局定义菜单)!
- 一些资料
- CDH4.1(hadoop-2.0.0-cdh4.1.2)安装部署文档
- iOS UIAlertView弹框
- 【error LNK2019: unresolved external symbol】
- 知道同样都是8小时,为什么待遇差这么多么
- 菜单之二:使用xml文件定义菜单
- 【Android基础入门〖1〗】UI布局
- uva-409
- hive的内部表与外部表创建
- mysql dba 网址
- 【Android基础入门〖2〗】Eclipse 导入 Android 源码
- Hive限制
- 开发者必备,超实用的PHP代码片段!
- [Unity3D]Unity3d开发常用代码集合