Add ActionBar Buttons and androidactionbar to Android
1. Create an Xml file and label menu in the res/menu folder, and set item
<? Xml version = "1.0" encoding = "UTF-8"?> <Myapp: menu xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: myapp = "http://schemas.android.com/apk/res-auto"> <! -- Id: button Id --> <! -- Icon: button icon --> <! -- Title: button text --> <! -- ShowAsAction: button display status --> <item android: id = "@ + id/buttonIcon" android: icon = "@ drawable/ic_near_me_white" android: title = "navigation" myapp: showAsAction = "always"/> </myapp: menu>
View Code
2. Reference this menu. xml in Activity
@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.buttons,menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.buttonIcon: Toast.makeText(OrderDetailActivity.this,"hello world",Toast.LENGTH_SHORT).show(); break; } return super.onOptionsItemSelected(item); }View Code
Android 3.0 or later
- Always-> always display in Action bar;
- IfRoom-> If the ActionBar has enough space, it is displayed;
- Never-> not displayed in Action Bar, collapsed in Over Flow;
- WithText-> the menu item is displayed with its icon and menu text)
Android 2.0 or later
Namespace needs to be introduced
- Add a custom namespace to menu. xml
Xmlns: myapp = "http://schemas.android.com/apk/res-auto"
- Specify the namespace before ShowAsAction
myapp:showAsAction="always"