使用XML添加SubMenu子菜单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.submenu"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="20" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Light.NoTitleBar" >
            
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>



XML:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.submenu.MainActivity" >

    <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>


package com.example.submenu;

import android.app.Activity;
import android.os.Bundle;
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.setHeaderTitle("文件操作");
//    	file.setHeaderIcon(R.drawable.ic_launcher);
//    	file.add(1,1,1,"新建");
//    	file.add(1,2,1,"打开");
//    	file.add(1,3,1,"保存");
//    	edit.setHeaderTitle("编辑操作");
//    	edit.setHeaderIcon(R.drawable.ic_launcher);
//    	edit.add(2,1,1,"复制");
//    	edit.add(2,2,1,"粘贴");
//    	edit.add(2,3,1,"剪切");
        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();
//        if (id == R.id.action_settings) {
//            return true;
//        }
    	switch (item.getItemId()) {
		case R.id.new_file:
			Toast.makeText(MainActivity.this,"点击了新建",Toast.LENGTH_SHORT).show();
			break;
case R.id.open_file:
	Toast.makeText(MainActivity.this,"点击了打开",Toast.LENGTH_SHORT).show();
			break;
case R.id.save_file:
	Toast.makeText(MainActivity.this,"点击了保存",Toast.LENGTH_SHORT).show();
	break;
case R.id.c_edit:
	Toast.makeText(MainActivity.this,"点击了复制",Toast.LENGTH_SHORT).show();
	break;
case R.id.v_edit:
	Toast.makeText(MainActivity.this,"点击了粘贴",Toast.LENGTH_SHORT).show();
	break;
case R.id.x_edit:
	Toast.makeText(MainActivity.this,"点击了剪切",Toast.LENGTH_SHORT).show();
	break;

		default:
			break;
		}
        return super.onOptionsItemSelected(item);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值