SubMenu---子菜单

Android.view.Menu

子菜单是某个菜单项的扩展,是一个悬浮的菜单列表。子菜单不支持菜单图标或者嵌套子菜单。

demo代码:

package com.imooc.summenudemo;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.SubMenu;
import android.widget.Toast;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		// getMenuInflater().inflate(R.menu.main, menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main, menu);
//		SubMenu file = menu.addSubMenu("文件");
//		SubMenu edit = menu.addSubMenu("编辑");
//		file.add(1, 1, 1, "新建");
//		file.add(1, 2, 1, "打开");
//		file.add(1, 3, 1, "保存");
//		file.setHeaderTitle("文件操作");//子菜单标题
//		file.setHeaderIcon(R.drawable.ic_launcher);//子菜单图标
//		edit.add(2, 1, 1, "复制");
//		edit.add(2, 2, 1, "粘贴");
//		edit.add(2, 3, 1, "剪切");
//		edit.setHeaderTitle("编辑操作");
//		edit.setHeaderIcon(R.drawable.ic_launcher);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.new_file:
			Toast.makeText(this, "点击了新建",Toast.LENGTH_SHORT).show();
			break;
		case R.id.open_file:
			Toast.makeText(this, "点击了打开",Toast.LENGTH_SHORT).show();
			break;
		case R.id.save_file:
			Toast.makeText(this, "点击了保存",Toast.LENGTH_SHORT).show();
			break;
		case R.id.c_edit:
			Toast.makeText(this, "点击了复制",Toast.LENGTH_SHORT).show();
			break;
		case R.id.v_edit:
			Toast.makeText(this, "点击了粘贴",Toast.LENGTH_SHORT).show();
			break;
		case R.id.x_edit:
			Toast.makeText(this, "点击了剪切",Toast.LENGTH_SHORT).show();
			break;
		}
		return super.onOptionsItemSelected(item);
	}
}
</pre><p></p><p><span style="font-size:18px">menu.main.xml</span></p><p><span style="font-size:18px"></span></p><pre code_snippet_id="1692353" snippet_file_name="blog_20160522_3_5287083" name="code" class="html"><menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:showAsAction="never"
        android:title="文件">
        <menu>
            <item
                android:id="@+id/new_file"
                android:showAsAction="never"
                android:title="新建"/>
            <item
                android:id="@+id/open_file"
                android:showAsAction="never"
                android:title="打开"/>
            <item
                android:id="@+id/save_file"
                android:showAsAction="never"
                android:title="保存"/>
        </menu>
    </item>
	 <item
        android:showAsAction="never"
        android:title="编辑">
        <menu>
            <item
                android:id="@+id/c_edit"
                android:showAsAction="never"
                android:title="复制"/>
            <item
                android:id="@+id/v_edit"
                android:showAsAction="never"
                android:title="粘贴"/>
            <item
                android:id="@+id/x_edit"
                android:showAsAction="never"
                android:title="剪切"/>
        </menu>
    </item>
</menu>





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值