When we use the Android phone, we often find that the Actionbar in the application differs greatly from the actionbar we normally use. Simply put, the other application of the Actionbar why so gorgeous, their own application of the Actionbar is so lame? Recently, there is time to carefully study the related issues about Actionbar.
First of all, let's take a look at what we said about "tall" Actionbar:
Read the Android documentation and the Actionbar implementation source code. Discover that Actionbar can be customized (ps:google or thoughtful, worthy of the Great God-level program ape). See here everyone's magic veil of Actionbar has been revealed. Let's take a look at the details of how it is implemented.
Package Com.jony.actionbarccustom;import Android.app.actionbar;import Android.app.activity;import Android.os.bundle;import Android.view.menu;import Android.view.menuitem;import Android.view.View;import Android.view.view.onclicklistener;import Android.widget.toast;public class Mainactivity extends Activity {@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Define yourself ActionBar final ActionBar ActionBar = Getactionbar (); Actionbar.setdisplayoptions (Actionbar.display_show_custom); Actionbar.setcustomview (R.layout.actionbar);//define your own ActionBar layout Actionbar.getcustomview (). Setonclicklistener (New Onclicklistener () {//listener event @Override public void OnClick (View arg0) {switch (arg0.get Id ()) {case r.id.back:showtoast (r.string.finish); Finish (); Break Default:break; } } }); } @Override Public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to the Actio n Bar if it is present. Getmenuinflater (). Inflate (R.menu.actionbar_menu, menu); Use code to dynamically control the display state of the MenuItem or XML definition MenuItem display state/* MenuItem share = Menu.finditem (R.id.share); MenuItem undo = Menu.finditem (R.id.undo); MenuItem redo = Menu.finditem (R.id.redo); MenuItem save = Menu.finditem (R.id.save); Share.setshowasaction (menuitem.show_as_action_always); Undo.setshowasaction (menuitem.show_as_action_always); Redo.setshowasaction (menuitem.show_as_action_always); Save.setshowasaction (menuitem.show_as_action_always); */return true; } @Override public boolean onoptionsitemselected (MenuItem item) {switch (Item.getitemid ()) {case R.I D.undo:showtoast (R.string.undo); Break Case R.id.redo:showtoast (R.string.redo); Break Case R.id.save:showtoast (R.string.save); Break Case R.id.share:showtoast (R.string.share); Break Default:break; } return super.onoptionsitemselected (item); } private void Showtoast (int msg) {Toast.maketext (Getapplicationcontext (), MSG, Toast.length_short). Show (); }}
All right. The detailed code is relatively simple and no longer elaborated, look at:
Source
Android for Actionbar customization