安卓开发_浅谈SubMenu(子菜单)

子菜单,即点击菜单后出现一个菜单栏供选择

创建子菜单的步骤:
  (1) 覆盖Activity的onCreateOptionsMenu()方法,调用Menu的addSubMenu()方法来添加子菜单
  (2) 调用SubMenu的add()方法,添加子菜单项
  (3) 覆盖onContextItemSelected()方法,响应子菜单的单击事件

示例:

(1) 覆盖Activity的onCreateOptionsMenu()方法,调用Menu的addSubMenu()方法来添加子菜单

        //动态添加子菜单

         SubMenu file = menu.addSubMenu("文件");//子菜单的名字
SubMenu edit = menu.addSubMenu("编辑");//子菜单的名字

(2) 调用SubMenu的add()方法,添加子菜单项

         //给子菜单添加菜单项
file.setHeaderTitle("文件操作");//菜单标题
file.setHeaderIcon(R.drawable.icon_72); //菜单图标
file.add(, , , "打开");
file.add(, , , "关闭");
file.add(, , , "删除"); edit.setHeaderTitle("编辑操作");
edit.setHeaderIcon(R.drawable.icon_72);
edit.add(,,,"复制");
edit.add(,,,"粘贴");
edit.add(,,,"剪切");

// 添加OptionsMenu菜单项,即4,5,6,10,11,12行代码方法
/* menu.add(groupId, itemId, order, title)
* groupId:菜单项所在的组
* itemId:菜单项编号
* order:排序
* title:标题
* setIcon()方法为菜单设置图标,

(3) 覆盖onContextItemSelected()方法,响应子菜单的单击事件

 //添加响应事件
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub //先判断组id再判断item 的id
if(item.getGroupId()==)
{
switch (item.getItemId()) {
case :
Toast.makeText(SubMenu_text.this, "点击了打开", ).show();
break;
case :
Toast.makeText(SubMenu_text.this, "点击了关闭", ).show();
break;
case :
Toast.makeText(SubMenu_text.this, "点击了删除", ).show();
break; default:
break;
}
}
else if(item.getGroupId()==)
{
switch (item.getItemId()) {
case :
Toast.makeText(SubMenu_text.this, "点击了复制", ).show();
break;
case :
Toast.makeText(SubMenu_text.this, "点击了粘贴", ).show();
break;
case :
Toast.makeText(SubMenu_text.this, "点击了剪切", ).show();
break; default:
break;
}
}
return super.onOptionsItemSelected(item);
}

完整代码:

 package information;

 import com.example.allcode.R;

 import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.widget.Toast; public class SubMenu_text extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.submenu);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
//动态添加子菜单 SubMenu file = menu.addSubMenu("文件");//子菜单的名字
SubMenu edit = menu.addSubMenu("编辑");//子菜单的名字 //给子菜单添加菜单项
file.setHeaderTitle("文件操作");//菜单标题
file.setHeaderIcon(R.drawable.icon_72); //菜单图标
file.add(, , , "打开");
file.add(, , , "关闭");
file.add(, , , "删除"); edit.setHeaderTitle("编辑操作");
edit.setHeaderIcon(R.drawable.icon_72);
edit.add(,,,"复制");
edit.add(,,,"粘贴");
edit.add(,,,"剪切"); return super.onCreateOptionsMenu(menu); } //添加响应事件
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub //先判断组id再判断item 的id
if(item.getGroupId()==)
{
switch (item.getItemId()) {
case :
Toast.makeText(SubMenu_text.this, "点击了打开", ).show();
break;
case :
Toast.makeText(SubMenu_text.this, "点击了关闭", ).show();
break;
case :
Toast.makeText(SubMenu_text.this, "点击了删除", ).show();
break; default:
break;
}
}
else if(item.getGroupId()==)
{
switch (item.getItemId()) {
case :
Toast.makeText(SubMenu_text.this, "点击了复制", ).show();
break;
case :
Toast.makeText(SubMenu_text.this, "点击了粘贴", ).show();
break;
case :
Toast.makeText(SubMenu_text.this, "点击了剪切", ).show();
break; default:
break;
}
}
return super.onOptionsItemSelected(item);
}
}

SubMenu_text.java

注意:为了实现界面的美化,需要给该activity一个样式

在AndroidManifest.xml文件下修改

<activity android:name="information.SubMenu_text"
  android:theme="@android:style/Theme.Light.NoTitleBar">
</activity>

效果图:

安卓开发_浅谈SubMenu(子菜单)

上一篇:STM32CubeMX使用教程——GPIO端口(上)


下一篇:Orders matters: seq2seq for set 实验