欢迎莅临 SUN WU GANG 的园子!!!

世上无难事,只畏有心人。有心之人,即立志之坚午也,志坚则不畏事之不成。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

Menu菜单资源

android应用推荐使用XML来定义菜单,其可提供更好的解耦方式。

菜单资源通常位于res/menu文件夹下,其菜单根元素为<menu.../>,menu元素下可包含子元素如下:

<item.../>子元素 定义菜单项
<group.../>子元素 将多个<item>定义的菜单项包装成一个菜单组
<group.../>子元素



用于控制整个菜单组的行为,该元素可指定如下常用属性:

checkableBehavior:指定该组菜单的行为,可指定为none(不可选)、all(多选)、single(单选)

menuCategory:对菜单进行分类,指定菜单的优先级。有效值为container、system、secondary、alternative

visible:指定改组菜单是否可见

enable:指定改组菜单是否可用

 

 

 

 

 

 

 

 

<item.../>元素用于定义一份菜单项,其又可包含<menu.../>元素,位于<item.../>内的<menu../>代表子菜单。

<item.../>元素可指定如下属性:

android:id 为菜单项指定唯一标识
android:title 指定菜单项标题
android:icon 指定菜单项图标
android:alphabeticShortcut 为菜单项指定字符快捷键
android:numericShortcut 为菜单项指定数字快捷键
android:checkable 设置菜单项是否可选
android:checked 设置菜单项是否已选中
android:visible 设置菜单项是否可见
android:enable 设置菜单项是否可用

 

 

 

 

 

 

 

 

注意:一旦在程序中定义菜单资源后,需要重写onCreateOptionsMenu(用于创建选项菜单)、onCreateContextMenu(用于创建上下文菜单),

在这些方法调用MenuInflater对象的inflate方法装载指定资源对应的菜单即可。

实例如下:

资源文件==》mymenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:icon="@drawable/font"
        android:title="@string/font_size">
        <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>
            </group>
        </menu>
    </item>
    <!-- 定义一个普通菜单 -->
    <item
        android:id="@+id/plain_item"
        android:title="@string/plant_item"/>
    <item
        android:icon="@drawable/color"
        android:title="@string/font_color">
        <menu>

            <!-- 定义一组运行复选的菜单项 -->
            <group>
                <item
                    android:id="@+id/red_font"
                    android:title="@string/font_red"/>
                <item
                    android:id="@+id/green_font"
                    android:title="@string/font_green"/>
                <item
                    android:id="@+id/blue_font"
                    android:title="@string/font_blue"/>
            </group>
        </menu>
    </item>

</menu>

context.xml==》
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- 定义一组单选菜单 -->
    <group android:checkableBehavior="single" >
        <item
            android:id="@+id/red1"
            android:title="@string/title_red"
            android:alphabeticShortcut="r"/>
        <item
            android:id="@+id/green1"
            android:title="@string/title_green"
            android:alphabeticShortcut="g"/>
        <item
            android:id="@+id/blue1"
            android:title="@string/title_blue"
            android:alphabeticShortcut="b"/>
    </group>

</menu>

布局文件==>
<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity" >

    <EditText
          android:id="@+id/tvtest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="110112119" />

</LinearLayout>
代码实现==》
package com.example.mymenu2;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity
{
	private EditText tv;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Log.i("swg", "aaaaaaaaaaaaaaaaaa");
		tv = (EditText) this.findViewById(R.id.tvtest);
		// 为文本框注册上下文菜单
		registerForContextMenu(tv);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		MenuInflater inflater = new MenuInflater(this);
		inflater.inflate(R.menu.context, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	// 每次创建上下文菜单时都会触发该方法
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
	{
		MenuInflater inflater = new MenuInflater(this);
		inflater.inflate(R.menu.mymenu, menu);
		menu.setHeaderIcon(R.drawable.oneowne);
		menu.setHeaderTitle("请选择背景色");
	}

	@Override
	// 上下文菜单中菜单项被单击时触发该方法
	public boolean onOptionsItemSelected(MenuItem item)
	{
		Log.i("swg", "111111111111111111111");
		Log.i("swg", "item.getItemId()====" + item.getItemId());

		switch (item.getItemId())
		{
		case R.id.red1:
			// case R.id.red_font:
			item.setChecked(true);
			tv.setBackgroundColor(Color.RED);
			tv.setTextColor(Color.RED);
			System.out.println("-------------------RED------------------");
			Log.i("swg", "aaaaaaaaaaaaaaaaaarrrrrrrrrrrrrrrrrrrr");
			break;
		case R.id.green1:
			item.setChecked(true);
			tv.setBackgroundColor(Color.GREEN);
			tv.setTextColor(Color.GREEN);
			System.out.println("-------------------GREEN------------------");
			Log.i("swg", "aaaaaaaaaaaaaaaaaaddddddddddddddddd");
			break;
		case R.id.blue1:
			item.setChecked(true);
			tv.setBackgroundColor(Color.BLUE);
			tv.setTextColor(Color.BLUE);
			System.out.println("-------------------BLUE------------------");
			Log.i("swg", "aaaaaaaaaaaaaaaaaabbbbbbbbbb");
			break;
		}
		return true;
	}

	// 菜单项被单击后的回调方法
	@Override
	public boolean onContextItemSelected(MenuItem item)
	{
		Log.i("swg", "2222222222222222");
		switch (item.getItemId())
		{
		case R.id.font_10:
			tv.setTextSize(10 * 2);
			break;
		case R.id.font_12:
			tv.setTextSize(12 * 2);
			break;
		case R.id.plain_item:
			Toast.makeText(MainActivity.this, "你单击了普通菜单", Toast.LENGTH_LONG).show();
			break;
		}
		return true;
	}

}

注意:长按可触发上下文方法;普通菜单需要处理onCreateOptionsMenu、onOptionsItemSelected;上下文菜单需要处理onCreateContextMenu、onContextItemSelected

运行效果:略。

 

posted on 2016-09-08 11:37  sunwugang  阅读(276)  评论(0)    收藏  举报