package com.example.submenu;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
final int FONT_10 = 0x123;
final int FONT_12 = 0x124;
final int FONT_14 = 0x125;
final int PLAIN_ITEM = 0x126;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViews();
}
private void findViews() {
textView = (TextView) findViewById(R.id.textView1);
}
// 单击menu时处罚
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, PLAIN_ITEM, 0, "普通菜单");
SubMenu progMenu = menu.addSubMenu("启动另一个activity");
progMenu.setIcon(R.drawable.ic_launcher);
progMenu.setHeaderIcon(R.drawable.ic_launcher);
progMenu.setHeaderTitle("启动");
MenuItem menuItem = progMenu.add("启动");
menuItem.setIntent(new Intent(MainActivity.this, OtherActivity.class));
SubMenu fontMenu = menu.addSubMenu("字体大小");
fontMenu.setIcon(R.drawable.ic_launcher);
fontMenu.setHeaderIcon(R.drawable.ic_launcher);
fontMenu.setHeaderTitle("选择字体大小");
fontMenu.add(0, FONT_10, 0, "10号");
fontMenu.add(0, FONT_12, 0, "12号");
fontMenu.add(0, FONT_14, 0, "14号");
return super.onCreateOptionsMenu(menu);
}
// 单击item时触发
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case FONT_10:
textView.setTextSize(10 * 2);
break;
case FONT_12:
textView.setTextSize(12 * 2);
break;
case FONT_14:
textView.setTextSize(14 * 2);
break;
case PLAIN_ITEM:
Toast.makeText(this, "普通菜单", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
final int FONT_10 = 0x123;
final int FONT_12 = 0x124;
final int FONT_14 = 0x125;
final int PLAIN_ITEM = 0x126;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViews();
}
private void findViews() {
textView = (TextView) findViewById(R.id.textView1);
}
// 单击menu时处罚
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, PLAIN_ITEM, 0, "普通菜单");
SubMenu progMenu = menu.addSubMenu("启动另一个activity");
progMenu.setIcon(R.drawable.ic_launcher);
progMenu.setHeaderIcon(R.drawable.ic_launcher);
progMenu.setHeaderTitle("启动");
MenuItem menuItem = progMenu.add("启动");
menuItem.setIntent(new Intent(MainActivity.this, OtherActivity.class));
SubMenu fontMenu = menu.addSubMenu("字体大小");
fontMenu.setIcon(R.drawable.ic_launcher);
fontMenu.setHeaderIcon(R.drawable.ic_launcher);
fontMenu.setHeaderTitle("选择字体大小");
fontMenu.add(0, FONT_10, 0, "10号");
fontMenu.add(0, FONT_12, 0, "12号");
fontMenu.add(0, FONT_14, 0, "14号");
return super.onCreateOptionsMenu(menu);
}
// 单击item时触发
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case FONT_10:
textView.setTextSize(10 * 2);
break;
case FONT_12:
textView.setTextSize(12 * 2);
break;
case FONT_14:
textView.setTextSize(14 * 2);
break;
case PLAIN_ITEM:
Toast.makeText(this, "普通菜单", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}