Android组件 子菜单(SubMenu)

     注意:可以在Menu上添加子菜单(SubMenu),但子菜单不能再嵌套子菜单,即意味着Android中菜单只有两层,这是项目设计时需要注意的。

    

     Java示例代码如下:

package com.demo.android.submenu;

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 SubMenuActivity extends Activity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		SubMenu fileMenu=menu.addSubMenu(1, 1, 1, "File");
		//fileMenu.setHeaderTitle("File");
		fileMenu.add(2, 11, 11, "New");
		fileMenu.add(2, 12, 12, "Save");
		fileMenu.add(2, 13, 13, "Close");

		SubMenu editMenu=menu.addSubMenu(1, 2, 2, "Edit");
		//editMenu.setIcon(icon)
		//editMenu.setHeaderTitle("");
		editMenu.add(2, 21, 21, "Redo");
		editMenu.add(2, 22, 22, "Undo");
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		switch(item.getItemId()){
			case 1:{
				Toast.makeText(SubMenuActivity.this, "单击了"+item.getTitle(), Toast.LENGTH_SHORT).show();
				break;
			}
			case 2:{
				Toast.makeText(SubMenuActivity.this, "单击了"+item.getTitle(), Toast.LENGTH_SHORT).show();
				break;
			}
			case 11:{
				Toast.makeText(SubMenuActivity.this, "单击了"+item.getTitle(), Toast.LENGTH_SHORT).show();
				break;
			}
			case 12:{
				Toast.makeText(SubMenuActivity.this, "单击了"+item.getTitle(), Toast.LENGTH_SHORT).show();
				break;
			}
			case 13:{
				Toast.makeText(SubMenuActivity.this, "单击了"+item.getTitle(), Toast.LENGTH_SHORT).show();
				break;
			}
			case 21:{
				Toast.makeText(SubMenuActivity.this, "单击了"+item.getTitle(), Toast.LENGTH_SHORT).show();
				break;
			}
			case 22:{
				Toast.makeText(SubMenuActivity.this, "单击了"+item.getTitle(), Toast.LENGTH_SHORT).show();
				break;
			}
		}

		return true;
	}


}


 

你可能感兴趣的:(java,android,File,Class,menu)