Android menu system

1.Icon menu icon&short text

2.Expaned menu

3.Submenus

 

 

create icon menu

 

public boolean onCreateOptionsMenu(Menu menu){

 

}

 

// Create a new check box item.
menu.add(0, CHECKBOX_ITEM, Menu.NONE, "CheckBox").setCheckable(true);
// Create a radio button group.
menu.add(RB_GROUP, RADIOBUTTON_1, Menu.NONE, "Radiobutton 1");
menu.add(RB_GROUP, RADIOBUTTON_2, Menu.NONE, "Radiobutton 2");
menu.add(RB_GROUP, RADIOBUTTON_3, Menu.NONE,
"Radiobutton 3").setChecked(true);
menu.setGroupCheckable(RB_GROUP, true, true);

 

Shortcut keys

menuItem.setShortcut(’0’, ‘b’);

 

Condensed titles

menuItem.setTitleCondensed("Short Title");

 

Icons

menuItem.setIcon(R.drawable.menu_item_icon);

 

Menu item click listener

 

menuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem _menuItem) {
[ ... execute click handling, return true if handled ... ]
return true;
}
});

 

Intents

menuItem.setIntent(new Intent(this, MyOtherActivity.class));

 

Dynamically Updating Menu Items

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuItem menuItem = menu.findItem(MENU_ITEM);
[ ... modify menu items ... ]
return true;
}

 

Handling Menu Selections

public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);

// Find which menu item has been selected
switch (item.getItemId()) {
// Check for each known menu item
case (MENU_ITEM):
[ ... Perform menu handler actions ... ]
return true;
}
// Return false if you have not handled the menu item.
return false;
}

 

Creating Submenus

SubMenu sub = menu.addSubMenu(0, 0, Menu.NONE, "Submenu");
sub.setHeaderIcon(R.drawable.icon);
sub.setIcon(R.drawable.icon);
MenuItem submenuItem = sub.add(0, 0, Menu.NONE, "Submenu Item");

 

Using Context Menus

@Override
public void onCreateContextMenu(ContextMenu menu) {
super.onCreateContextMenu(menu);
menu.add("ContextMenuItem1");
}

 

The more common alternative is to create Activity-specific Context Menus by overriding the
Activity’s onCreateContextMenu method, and registering the Views that should use it using the registerForContextMenu as shown

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EditText view = new EditText(this);
setContentView(view);
registerForContextMenu(view);
}

 

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Context Menu");
menu.add(0, menu.FIRST, Menu.NONE,
"Item 1").setIcon(R.drawable.menu_item);
menu.add(0, menu.FIRST+1, Menu.NONE, "Item 2").setCheckable(true);
menu.add(0, menu.FIRST+2, Menu.NONE, "Item 3").setShortcut(’3’, ‘3’);
SubMenu sub = menu.addSubMenu("Submenu");
sub.add("Submenu Item");
}

 

 

Handling Context Menu Selections

@Override
public boolean onContextItemSelected(MenuItem item) {
super.onContextItemSelected(item);
[ ... Handle menu item selection ... ]
return false;
}

 

To use your Menu resource, use the MenuInflator class within your onCreateOptionsMenu or
onCreateContextMenu event handlers

 

<menu xmlns:android="http://schemas.android.com/apk/res/android"
android:name="Context Menu">
<item
android:id="@+id/item01"

android:icon="@drawable/menu_item"
android:title="Item 1">
</item>

</menu>

阅读更多
个人分类: Android
想对作者说点什么? 我来说一句

android 自定义菜单

2018年01月24日 16.63MB 下载

android 另类menu菜单提示

2011年09月09日 43KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭