子菜单,即点击菜单后出现一个菜单栏供选择
创建子菜单的步骤:
(1) 覆盖Activity的onCreateOptionsMenu()方法,调用Menu的addSubMenu()方法来添加子菜单
(2) 调用SubMenu的add()方法,添加子菜单项
(3) 覆盖onContextItemSelected()方法,响应子菜单的单击事件
示例:
(1) 覆盖Activity的onCreateOptionsMenu()方法,调用Menu的addSubMenu()方法来添加子菜单
1 //动态添加子菜单 2 3 SubMenu file = menu.addSubMenu("文件");//子菜单的名字 4 SubMenu edit = menu.addSubMenu("编辑");//子菜单的名字 5
(2) 调用SubMenu的add()方法,添加子菜单项
1 //给子菜单添加菜单项 2 file.setHeaderTitle("文件操作");//菜单标题 3 file.setHeaderIcon(R.drawable.icon_72); //菜单图标 4 file.add(1, 100, 1, "打开"); 5 file.add(1, 101, 1, "关闭"); 6 file.add(1, 102, 1, "删除"); 7 8 edit.setHeaderTitle("编辑操作"); 9 edit.setHeaderIcon(R.drawable.icon_72); 10 edit.add(2,100,1,"复制"); 11 edit.add(2,101,1,"粘贴"); 12 edit.add(2,102,1,"剪切");
// 添加OptionsMenu菜单项,即4,5,6,10,11,12行代码方法
/* menu.add(groupId, itemId, order, title)
* groupId:菜单项所在的组
* itemId:菜单项编号
* order:排序
* title:标题
* setIcon()方法为菜单设置图标,
(3) 覆盖onContextItemSelected()方法,响应子菜单的单击事件
1 //添加响应事件 2 @Override 3 public boolean onOptionsItemSelected(MenuItem item) { 4 // TODO Auto-generated method stub 5 6 //先判断组id再判断item 的id 7 if(item.getGroupId()==1) 8 { 9 switch (item.getItemId()) { 10 case 100: 11 Toast.makeText(SubMenu_text.this, "点击了打开", 1).show(); 12 break; 13 case 101: 14 Toast.makeText(SubMenu_text.this, "点击了关闭", 1).show(); 15 break; 16 case 102: 17 Toast.makeText(SubMenu_text.this, "点击了删除", 1).show(); 18 break; 19 20 default: 21 break; 22 } 23 } 24 else if(item.getGroupId()==2) 25 { 26 switch (item.getItemId()) { 27 case 100: 28 Toast.makeText(SubMenu_text.this, "点击了复制", 1).show(); 29 break; 30 case 101: 31 Toast.makeText(SubMenu_text.this, "点击了粘贴", 1).show(); 32 break; 33 case 102: 34 Toast.makeText(SubMenu_text.this, "点击了剪切", 1).show(); 35 break; 36 37 default: 38 break; 39 } 40 } 41 return super.onOptionsItemSelected(item); 42 }
完整代码:
1 package information; 2 3 import com.example.allcode.R; 4 5 import android.app.Activity; 6 import android.os.Bundle; 7 import android.view.Menu; 8 import android.view.MenuItem; 9 import android.view.SubMenu; 10 import android.widget.Toast; 11 12 public class SubMenu_text extends Activity{ 13 @Override 14 protected void onCreate(Bundle savedInstanceState) { 15 // TODO Auto-generated method stub 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.submenu); 18 } 19 20 @Override 21 public boolean onCreateOptionsMenu(Menu menu) { 22 // TODO Auto-generated method stub 23 //动态添加子菜单 24 25 SubMenu file = menu.addSubMenu("文件");//子菜单的名字 26 SubMenu edit = menu.addSubMenu("编辑");//子菜单的名字 27 28 //给子菜单添加菜单项 29 file.setHeaderTitle("文件操作");//菜单标题 30 file.setHeaderIcon(R.drawable.icon_72); //菜单图标 31 file.add(1, 100, 1, "打开"); 32 file.add(1, 101, 1, "关闭"); 33 file.add(1, 102, 1, "删除"); 34 35 edit.setHeaderTitle("编辑操作"); 36 edit.setHeaderIcon(R.drawable.icon_72); 37 edit.add(2,100,1,"复制"); 38 edit.add(2,101,1,"粘贴"); 39 edit.add(2,102,1,"剪切"); 40 41 return super.onCreateOptionsMenu(menu); 42 43 } 44 45 //添加响应事件 46 @Override 47 public boolean onOptionsItemSelected(MenuItem item) { 48 // TODO Auto-generated method stub 49 50 //先判断组id再判断item 的id 51 if(item.getGroupId()==1) 52 { 53 switch (item.getItemId()) { 54 case 100: 55 Toast.makeText(SubMenu_text.this, "点击了打开", 1).show(); 56 break; 57 case 101: 58 Toast.makeText(SubMenu_text.this, "点击了关闭", 1).show(); 59 break; 60 case 102: 61 Toast.makeText(SubMenu_text.this, "点击了删除", 1).show(); 62 break; 63 64 default: 65 break; 66 } 67 } 68 else if(item.getGroupId()==2) 69 { 70 switch (item.getItemId()) { 71 case 100: 72 Toast.makeText(SubMenu_text.this, "点击了复制", 1).show(); 73 break; 74 case 101: 75 Toast.makeText(SubMenu_text.this, "点击了粘贴", 1).show(); 76 break; 77 case 102: 78 Toast.makeText(SubMenu_text.this, "点击了剪切", 1).show(); 79 break; 80 81 default: 82 break; 83 } 84 } 85 return super.onOptionsItemSelected(item); 86 } 87 }
SubMenu_text.java
注意:为了实现界面的美化,需要给该activity一个样式
在AndroidManifest.xml文件下修改
<activity android:name="information.SubMenu_text"
android:theme="@android:style/Theme.Light.NoTitleBar">
</activity>
效果图:
时间: 2024-10-27 15:44:38