也没什么了 就是一个 android菜单的 小例子 实现步骤如下:
(1)创建一个包含文本视图的XML布局文件。
(2)创建一个Activity类,其中包含在第一步中定义的布局。
(3)设置菜单
(4)向菜单添加一些常规菜单项
(5)向菜单添加一些辅助菜单项
(6)响应菜单项
(7)修改 AndroidManifest.xml文件,以显示应用程序正确的标题

布局文件
<?xml version="1.0" encoding="utf-8"?>
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:id="@+id/textViewId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Debugging Scratch Pad"
/>



Activity类
package xiaohang.zhimeng;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class SampleMenusActivity extends Activity {

Menu myMenu = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

//重写onCreateOptionsMenu 并以编程方式设置菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// call the parent to attach any system level menus
super.onCreateOptionsMenu(menu);
this.myMenu = menu;

//add a few normal menus
addRegularMenuItems(menu);

//add a few secondary menus
add5SecondaryMenuItems(menu);

//it must return true to show the menu
//if it is false menu won't show
return true;

}

//添加常规菜单项
private void addRegularMenuItems(Menu menu){
int base = Menu.FIRST; // value is 1

menu.add(base, base, base, "append");
menu.add(base, base+1, base+1, "item 2");
menu.add(base, base+2, base+2, "clear");

menu.add(base, base+3, base+3, "hide secondary");
menu.add(base, base+4, base+4, "show secondary");

menu.add(base, base+5, base+5, "enable secondary");
menu.add(base, base+6, base+6, "disable secondary");

menu.add(base, base+7, base+7, "check secondary");
menu.add(base, base+8, base+8, "uncheck secondary");
}

//添加辅助菜单项
private void add5SecondaryMenuItems(Menu menu){
//Secondary items are shown just like everything else
int base=Menu.CATEGORY_SECONDARY;

menu.add(base, base+1, base+1, "sec. item 1");
menu.add(base, base+2, base+2, "sec. item 2");
menu.add(base, base+3, base+3, "sec. item 3");
menu.add(base, base+4, base+4, "sec. item 4");
menu.add(base, base+5, base+5, "sec. item 5");

}

//响应菜单项单击
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == 1) {
appendText("\nhello");
}
else if (item.getItemId() ==2) {
appendText("\nitem2");
}
else if (item.getItemId() == 3) {
emptyText();
}
else if (item.getItemId() ==4) {
//hide secondary
this.appendMenuItemText(item);
this.myMenu.setGroupVisible(Menu.CATEGORY_SECONDARY, false);
}
else if(item.getItemId() == 5){
//show secondary
this.appendMenuItemText(item);
this.myMenu.setGroupVisible(Menu.CATEGORY_SECONDARY, true);
}
else if (item.getItemId() == 6) {
//enable secondary
this.appendMenuItemText(item);
this.myMenu.setGroupEnabled(Menu.CATEGORY_SECONDARY, true);
}
else if(item.getItemId() == 7){
//disable secondary
this.appendMenuItemText(item);
this.myMenu.setGroupEnabled(Menu.CATEGORY_SECONDARY, false);
}
else if (item.getItemId() == 8) {
//check secondary
this.appendMenuItemText(item);
myMenu.setGroupCheckable(Menu.CATEGORY_SECONDARY, true, false);
}
else if (item.getItemId() == 9) {
//uncheck secondary
this.appendMenuItemText(item);
myMenu.setGroupCheckable(Menu.CATEGORY_SECONDARY, false, false);
}
else {
this.appendMenuItemText(item);
}
//should return true if the menu item
//is handled
return true;

}


//向调试TextView 写入数据的实用程序函数
//Given a string of text append it to the TextView
private void appendText(String text){
TextView tv = (TextView)this.findViewById(R.id.textViewId);
tv.setText(tv.getText() + text);
}

//Given a menu item append its title to the TextView
private void appendMenuItemText(MenuItem menuItem){
String title = menuItem.getTitle().toString();
TextView tv = (TextView)this.findViewById(R.id.textViewId);
tv.setText(tv.getText() + "\n" + title);
}

//Empty the TextView of its contents
private void emptyText(){
TextView tv = (TextView)this.findViewById(R.id.textViewId);
tv.setText("");
}
}


AndroidManifest.xml文件
<?xml version="1.0" encoding="utf-8"?>
package="xiaohang.zhimeng"
android:versionCode="1"
android:versionName="1.0">

android:label="Sample Menus Application">











运行效果如下

[img]http://dl.iteye.com/upload/attachment/461776/80e59777-03a9-3bbd-b5a5-ab705ba38c10.jpg[/img]


[img]http://dl.iteye.com/upload/attachment/461778/65821ac3-ab62-3e67-a2e9-3a6e05980154.jpg[/img]

源码附件

更多相关文章

  1. android操作sdcard中的多媒体文件(一)——音乐列表的制作
  2. android 操作sdcard中的多媒体文件(一)——音乐列表的制作
  3. Android性能优化之布局优化
  4. Android中动态初始化布局参数以及ConstraintLayout使用中遇到的
  5. Android之布局属性重点
  6. Android Zip文件解压缩代码
  7. android 布局如何支持多种不同屏幕尺寸
  8. Android中多媒体文件、文档以及各类文件的获取
  9. Android 超简单的录制屏幕视频及生成GIF文件的方法

随机推荐

  1. eclipse下android的sdk配置问题
  2. 我是如何自学Android,资料分享(2015 版)
  3. Android中的Shape美化
  4. Android(安卓)TextView文字横向自动滚动(
  5. Android 技术专题系列之三 -- 编译(build)
  6. Android 相对布局:RelativeLayout
  7. Android分区解释:boot, system, recovery,
  8. Android(安卓)init 启动过程分析1
  9. Android平板上开发应用的一点心得——精
  10. 使用Lint 和 Annotations来提升代码质量