Demo02-2:创建Option菜单

来源:互联网 发布:蛙泳世界纪录 知乎 编辑:程序博客网 时间:2024/06/05 03:11
package com.wuqiong.demo02_2;import android.app.Activity;import android.graphics.Color;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.SubMenu;import android.widget.EditText;public class MenuTest extends Activity {private EditText edit;final int BACK_1 = 0x111;final int BACK_2 = 0x112;final int BACK_3 = 0x113;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_menu_test);        edit = (EditText) findViewById(R.id.txt);    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.menu_test, menu);        SubMenu backMenu = menu.addSubMenu("背景颜色");        backMenu.add(0, BACK_1, 0, "红色");        backMenu.add(0, BACK_2, 0, "蓝色");        backMenu.add(0, BACK_3, 0, "绿色");        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        switch(id){        case R.id.font_10:        edit.setTextSize(10*2);        break;        case R.id.font_12:        edit.setTextSize(12*2);        break;        case R.id.font_14:        edit.setTextSize(14*2);        break;        case R.id.font_16:        edit.setTextSize(16*2);        break;        case R.id.font_red:        edit.setTextColor(Color.RED);        break;        case R.id.font_blue:        edit.setTextColor(Color.BLUE);        break;        case R.id.font_green:        edit.setTextColor(Color.GREEN);        break;        case BACK_1:        edit.setBackgroundColor(Color.RED);        break;        case BACK_2:        edit.setBackgroundColor(Color.BLUE);        break;        case BACK_3:        edit.setBackgroundColor(Color.GREEN);        break;        default:        break;        }        return true;    }}


<menu xmlns:android="http://schemas.android.com/apk/res/android" ><item android:id="@+id/fontsize"      android:title="字体大小"       android:icon="@drawable/size" ><menu>    <group android:checkableBehavior="single">    <item android:id="@+id/font_10"              android:title="10号字体"/>    <item android:id="@+id/font_12"              android:title="12号字体"/>    <item android:id="@+id/font_14"              android:title="14号字体"/>    <item android:id="@+id/font_16"              android:title="16号字体"/>    </group></menu></item><item android:id="@+id/fontcolor"      android:title="字体颜色"       android:icon="@drawable/color" ><menu>    <group>    <item android:id="@+id/font_red"              android:title="红色"/>    <item android:id="@+id/font_blue"              android:title="蓝色"/>    <item android:id="@+id/font_green"              android:title="绿色"/>    </group></menu></item></menu>

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <EditText         android:id="@+id/txt"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="用于测试的内容" /></LinearLayout>


0 0
原创粉丝点击