通过xml文件定义菜单
来源:互联网 发布:剑三南风正太捏脸数据 编辑:程序博客网 时间:2024/06/05 18:26
通过xml文件定义菜单,并在Activity中引用菜单资源,并为菜单项绑定监听器,实现为TextView设置各种属性。
上下文菜单的xml资源文件,context.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <group android:checkableBehavior="single" > <item android:id="@+id/red" android:title="@string/red_title" android:alphabeticShortcut="r" ></item> <item android:id="@+id/green" android:title="@string/green_title" android:alphabeticShortcut="g" ></item> <item android:id="@+id/blue" android:title="@string/blue_title" android:alphabeticShortcut="b" ></item> </group> </menu>
菜单项资源文件,my_menu.xml:
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:title="@string/font_size" android:icon="@drawable/font" > <menu> <!-- 定义一组菜单选项 --> <group android:checkableBehavior="single" > <item android:id="@+id/font_10" android:title="@string/font_10" ></item> <item android:id="@+id/font_12" android:title="@string/font_12" ></item> <item android:id="@+id/font_14" android:title="@string/font_14" ></item> <item android:id="@+id/font_16" android:title="@string/font_16" ></item> <item android:id="@+id/font_18" android:title="@string/font_18" ></item> </group> </menu> </item> <!-- 定义一个普通菜单项 --><item android:id="@+id/plain_item" android:title="@string/plain_item" /><item android:id="@+id/font_color" android:title="@string/font_color" android:icon="@drawable/color" > <menu> <!-- 定义一组允许复选的菜单项 --> <group android:checkableBehavior="single"> <item android:id="@+id/red" android:title="@string/red_title" /> <item android:id="@+id/green" android:title="@string/green_title" /> <item android:id="@id/blue" android:title="@string/blue_title" /> </group> </menu></item></menu>
Activity代码:
package com.example.menurestest;import android.os.Bundle;public class MenuResTest extends Activity {private TextView txt; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_menu_res_test); txt=(TextView)findViewById(R.id.txt); //为文本框注册上下文菜单 registerForContextMenu(txt); } @Override public boolean onCreateOptionsMenu(Menu menu) { //添加菜单资源的方法一 getMenuInflater().inflate(R.menu.my_menu, menu); return true; } //每次 创建上下文菜单时都会触发该方法@Overridepublic void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {// TODO Auto-generated method stub//添加菜单资源的方法二MenuInflater inflater=new MenuInflater(this);inflater.inflate(R.menu.context, menu);menu.setHeaderIcon(R.drawable.tools);menu.setHeaderTitle("请选择背景色");menu.setGroupCheckable(1, true, true);}//上下文菜单项被单击时触发该方法 @Overridepublic boolean onContextItemSelected(MenuItem item) {// TODO Auto-generated method stub switch(item.getItemId()){ case R.id.red: item.setChecked(true); txt.setBackgroundColor(Color.RED); break; case R.id.green: item.setChecked(true); txt.setBackgroundColor(Color.GREEN); break; case R.id.blue: item.setChecked(true); txt.setBackgroundResource(Color.BLUE); break; }return super.onContextItemSelected(item);} @Overridepublic boolean onOptionsItemSelected(MenuItem item) {// TODO Auto-generated method stub switch(item.getItemId()){ case R.id.font_10: item.setChecked(true); txt.setTextSize(10*2); break; case R.id.font_12: item.setChecked(true); txt.setTextSize(12*2); break; case R.id.font_14: item.setChecked(true); txt.setTextSize(14*2); break; case R.id.font_16: item.setChecked(true); txt.setTextSize(16*2); break; case R.id.font_18: txt.setTextSize(18*2); item.setChecked(true);item.setChecked(true); break; case R.id.red: txt.setTextColor(Color.RED); item.setChecked(true); break; case R.id.blue: txt.setTextColor(Color.BLUE); item.setChecked(true); break; case R.id.green: item.setChecked(true); txt.setTextColor(R.color.green); break; case R.id.plain_item: Toast.makeText(MenuResTest.this, "您单击了普通菜单", Toast.LENGTH_SHORT).show(); break; }return super.onOptionsItemSelected(item);} }运行结果:
- 通过xml文件定义菜单
- 定义XML菜单文件
- 菜单之二:使用xml文件定义菜单
- 菜单之二:使用xml文件定义菜单
- 【xml array】通过xml资源文件定义数组
- 通过XML定义Spinner
- Android中通过xml资源文件定义数组
- Android中通过xml资源文件定义数组
- Android中通过xml资源文件定义数组
- Android中通过xml资源文件定义数组
- Android中通过xml资源文件定义数组
- Android中通过xml资源文件定义数组
- Android中通过xml资源文件定义数组
- Android中通过xml资源文件定义数组
- Android中通过xml资源文件定义数组
- 在XML中定义菜单
- 在XML中定义菜单
- 使用 XML 定义选项菜单
- 命令查询网址
- Spring MVC
- 接口 抽象类
- 《编程珠玑》作者Jon Bentley推荐的书
- AS3变量作用域问题
- 通过xml文件定义菜单
- Eclipse Indigo中安装UML2 Tools
- 猜数字游戏···看看你对数字的敏感度,试试你最少几次猜中目标数字···
- hust 1024 dance party
- oracle数据类型详解
- RAID5数据恢复
- 【Android API指南】App组件(5) - Activities(3) - 任务和回退堆栈
- Window命令行常用工具(持续更新)
- C语言的谜题