<?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);
}
}