菜单资源的进一步使用

随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)

以下实例包括菜单在代码中创建和XML中创建两种方式,其中上下文菜单在XML中创建。

Activity:

package com.lovo;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.Button;
import android.widget.LinearLayout;

public class MenuActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.main);
		LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
		// 注册上下文菜单
		this.registerForContextMenu(layout);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// 在代码中创建菜单
		SubMenu file_menu = menu.addSubMenu("文件");
		file_menu.add(1, 11, 1, "新建");
		file_menu.add(1, 12, 2, "保存");
		file_menu.add(1, 13, 3, "另存为");
		SubMenu set_menu = menu.addSubMenu("设置");
		// 设置菜单为单选,第一个参数为组ID,第二个参数true表示可以勾选,第三个参数true表示可以单选,false表示可以多选
		set_menu.setGroupCheckable(2, true, true);
		set_menu.add(2, 21, 1, "红色");
		set_menu.add(2, 22, 2, "蓝色");
		set_menu.add(2, 23, 3, "绿色");
		menu.add(0, 3, 3, "关于");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
		switch (item.getItemId()) {
		case 21:
			layout.setBackgroundColor(Color.RED);// 设置背景为红色
			break;
		case 22:
			layout.setBackgroundColor(Color.BLUE);// 设置背景为蓝色
			break;
		case 23:
			layout.setBackgroundColor(Color.GREEN);// 设置背景为绿色
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		getMenuInflater().inflate(R.menu.content_menu, menu);
		super.onCreateContextMenu(menu, v, menuInfo);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		Button btn = (Button) findViewById(R.id.btn);
		switch (item.getItemId()) {
		case R.id.font_min:
			btn.setTextSize(10);
			break;
		case R.id.font_nomal:
			btn.setTextSize(15);
			break;
		case R.id.font_large:
			btn.setTextSize(20);
			break;
		default:
			break;
		}
		return super.onContextItemSelected(item);
	}
}


 


布局XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="测试菜单资源的使用" />

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示字体大小变化的按钮" />

</LinearLayout>


上下文菜单XML:

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

    <item
        android:id="@+id/font_min"
        android:title="小号字体"/>
    <item
        android:id="@+id/font_nomal"
        android:title="中号字体"/>
    <item
        android:id="@+id/font_large"
        android:title="大号字体"/>

</menu>


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

u010142437

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值