Android ActionBar and menu code settings style, androidactionbar
Xml Code of menu
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <menu xmlns: android = "http://schemas.android.com/apk/res/android"> 3 4 <item android: id = "@ + id/action_search" 5 android: title = "search 1" 6 android: orderInCategory = "100" 7 android: showAsAction = "always"/> 8 9 <item android: id = "@ + id/action_search2" 10 android: actionViewClass = "android. widget. searchView "11 android: showAsAction =" ifRoom | collapseActionView "12 android: orderInCategory =" 100 "13 android: title =" Search 2 "/> 14 15 16 <item android: id = "@ + id/action_share" 17 android: title = "share" 18 android: orderInCategory = "100" 19 android: icon = "@ drawable/ic_action_favor_normal" 20 android: showAsAction = "never"/> 21 <item android: id = "@ + id/action_collection" 22 android: title = "favorites" 23 android: orderInCategory = "100" 24 android: showAsAction = "never"/> 25 <item android: id = "@ + id/action_font" 26 android: title = "font size" 27 android: orderInCategory = "100" 28 android: showAsAction = "never"/> 29 </menu>
Overflower Menu icon display implementation in Menu [rewrite the onMenuOpened method and use the reflection principle]
1/** 2 * display overflower Menu icon 3 */4 @ Override 5 public boolean onMenuOpened (int featureId, menu Menu) {6 if (featureId = Window. FEATURE_ACTION_BAR & menu! = Null) {7 if (menu. getClass (). getSimpleName (). equals ("MenuBuilder") {8 try {9 Method m = menu. getClass (). getDeclaredMethod ("setOptionalIconsVisible", Boolean. TYPE); 10 m. setAccessible (true); 11 m. invoke (menu, true); 12} catch (Exception e) {13} 14} 15} 16 return super. onMenuOpened (featureId, menu); 17}
Event listening operations for options in the Menu
1/** 2 * menu click the listening event of the operation. 3 */4 @ Override 5 public boolean onOptionsItemSelected (MenuItem item) {6 switch (item. getItemId () {7 case android. r. id. home: 8 // finish (); 9 super. onBackPressed (); 10 break; 11 case R. id. action_add: 12 Toast. makeText (this, "add", Toast. LENGTH_SHORT ). show (); 13 break; 14} 15 return super. onOptionsItemSelected (item); 16}
Custom ActionBar
1/** 2 * initialize ActionBar content 3 **/4 private ActionBar actionBar; 5 private void initActionBar () {6 actionBar = super. getActionBar (); 7 actionBar. show (); 8 9 // display 10 actionBar in the Home area. setDisplayShowHomeEnabled (true); 11 // you can specify 12 actionBar for the home region. setDisplayHomeAsUpEnabled (true); 13 actionBar. setHomeAsUpIndicator (R. drawable. back_move_details_normal); 14 15 // The Home region Title 16 actionBar is not displayed. setDisplayShowTitleEnabled (true); // 17 actionBar. setTitle ("news"); // set title18 // do not display Logo picture 19 actionBar. setDisplayUseLogoEnabled (false); // 20 // remove the Icon in the home area [set the icon color to transparent] 21 Drawable colorDrawable = new 22 ColorDrawable (android. r. color. transparent); 23 actionBar. setIcon (colorDrawable); 24 25 // custom region 26 actionBar. setDisplayShowCustomEnabled (true); 27 TextView tvTitle = new TextView (this); // this, currently carrying 28 tvTitle. setText ("news"); // tvTitle. setId (); 29 tvTitle. setTextSize (25); 30 int colorVal = getResources (). getColor (R. color. white); 31 tvTitle. setTextColor (colorVal); // tvTitle. setTextColor (Color. WHITE); 32 tvTitle. setGravity (Gravity. CENTER); 33 34 LayoutParams layoutParams = 35 new LayoutParams (LayoutParams. MATCH_PARENT, LayoutParams. MATCH_PARENT); 36 actionBar. setCustomView (tvTitle, layoutParams); 37 38}
Effect: