1 package com.example.android_menu; 2 3 import android.app.Activity; 4 import android.graphics.Color; 5 import android.os.Bundle; 6 import android.view.Menu; 7 import android.view.MenuInflater; 8 import android.view.MenuItem; 9 import android.widget.TextView;10 11 public class MyMenuActivity extends Activity {12 TextView textView;13 14 @Override15 protected void onCreate(Bundle savedInstanceState) {16 super.onCreate(savedInstanceState);17 setContentView(R.layout.contextmenuactivity);18 textView = (TextView) findViewById(R.id.itemtext);19 }20 21 /**22 * 点击菜单按键时调用的函数23 */24 @Override25 public boolean onCreateOptionsMenu(Menu menu) {26 // 菜单加载器27 MenuInflater menuInflater = new MenuInflater(getApplicationContext());28 menuInflater.inflate(R.menu.mymenu, menu);29 return super.onCreateOptionsMenu(menu);30 }31 32 /**33 * 响应菜单动作的回调函数34 */35 @Override36 public boolean onOptionsItemSelected(MenuItem item) {37 //得到Id38 switch (item.getItemId()) {39 case R.id.font10sp:40 textView.setTextSize(10);41 break;42 case R.id.font20sp:43 textView.setTextSize(20);44 break;45 case R.id.font30sp:46 textView.setTextSize(30);47 break;48 case R.id.fontblue:49 textView.setTextColor(Color.BLUE);50 break;51 case R.id.fontgreen:52 textView.setTextColor(Color.GREEN);53 break;54 case R.id.fontred:55 textView.setTextColor(Color.RED);56 break;57 }58 return super.onOptionsItemSelected(item);59 }60 }
res/menu/mymenu.xml
1 2
点击Menu按键就会在屏幕底部弹出Menu