<?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>
package com.example.submenu;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
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);
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;
// }
if(item.getGroupId()==1){
switch (item.getItemId()) {
case 1:
Toast.makeText(MainActivity.this,"点击了新建",Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(MainActivity.this,"点击了打开",Toast.LENGTH_SHORT).show();
break;
case 3:
Toast.makeText(MainActivity.this,"点击了保存",Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
else if(item.getGroupId()==2){
switch (item.getItemId()) {
case 1:
Toast.makeText(MainActivity.this,"点击了复制",Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(MainActivity.this,"点击了粘贴",Toast.LENGTH_SHORT).show();
break;
case 3:
Toast.makeText(MainActivity.this,"点击了剪切",Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
return super.onOptionsItemSelected(item);
}
}
还没有评论,来说两句吧...