Use of Android Actionbar

Source: Internet
Author: User
Tags home screen


 action Bar main features include:
  1. Display the Options menu BR style= "font-family:arial; font-size:14px; line-height:26px ">  2. Provide navigation function for the switch mode of the tab page , able to switch multiple fragment. ,
  3.  provides a drop-down navigation entry.
  4. Provides an interactive active view instead of an option entry  
  5. Use the program's icon to return to the home screen or navigate up.


First of all, when using overflow, you need to call the OnCreate () function in the following way:

private void Forceshowoverflowmenu () {      try {          viewconfiguration config = viewconfiguration.get (this);          Field Menukeyfield = Viewconfiguration.class                  . Getdeclaredfield ("Shaspermanentmenukey");          if (Menukeyfield! = null) {              menukeyfield.setaccessible (true);              Menukeyfield.setboolean (config, false);          }      } catch (Exception e) {          e.printstacktrace ();      }  }

Note: This implementation is not used here. The popupmen used in this application


Here are the actionbar used by the projects that you have developed:

public class Shoporderactivity extends Actionbaractivity Implementsontouchlistener, onmenuitemclicklistener{private Toast mtoast;private Context context;/** alertdialog input Feedback box */private EditText et_feedback;private popupmenu popupmenu;@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.shop_order);//Set Home icon area//requestwindowfeature (Window.feature_left_icon);//setfeaturedrawableresource (Window.feature_left_icon, resId); this.context = This;mtoast = Toast.maketext (This, "", Toast.length_long); Initactionbar ();} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.actionbar_menu, menu); return true;} @Overridepublic boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here. The action bar will//automatically handle clicks on the Home/up button so long//as you specify a parent activity in and RoIdManifest.xml.int id = item.getitemid (), if (id = = R.id.actionbar_overflow) {if (PopupMenu = = null) {PopupMenu = new Popup Menu (This,findviewbyid (r.id.actionbar_overflow));p opupmenu.inflate (R.menu.actionbar_pop); Popupmenu.setonmenuitemclicklistener (this);} Popupmenu.show (); return true;} else if (<pre name= "code" class= "Java" >public class Shoporderactivity extends Actionbaractivity Implementsontouchlistener, onmenuitemclicklistener{private Toast mtoast;private Context context;/** Alertdialog Input Feedback box */private EditText et_feedback;private popupmenu popupmenu; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.shop_order);//Set Home icon Area// Requestwindowfeature (Window.feature_left_icon);//setfeaturedrawableresource (Window.feature_left_icon, resId); This.context = This;mtoast = Toast.maketext (This, "", Toast.length_long); Initactionbar ();} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; this adds iteMs to the action Bar if it is present.getmenuinflater (). Inflate (R.menu.actionbar_menu, menu); return true;} @Overridepublic boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here. The action bar will//automatically handle clicks on the Home/up button so long//as you specify a parent activity in and RoidManifest.xml.int id = item.getitemid (), if (id = = R.id.actionbar_overflow) {if (PopupMenu = = null) {PopupMenu = new Pop Upmenu (This,findviewbyid (r.id.actionbar_overflow));p opupmenu.inflate (R.menu.actionbar_pop); Popupmenu.setonmenuitemclicklistener (this);} Popupmenu.show (); return true;} else if (id = = Android. R.id.home) {finish ();} else if (id = = R.id.new_order_bar) {showtip ("Show Red dots");} return super.onoptionsitemselected (item);} @Overridepublic boolean Onmenuitemclick (MenuItem arg0) {Intent intent;switch (Arg0.getitemid ()) {case R.id.actionbar_ Settings:intent = new Intent (this, settingsactivity.class); startactivity (intent); Break;case R.id.historyorder: Intent = new INtent (this, historyorderactivity.class); startactivity (intent); Break;case R.id.logout:alertdialog.builder Builder = New Alertdialog.builder (Shoporderactivity.this); Builder.settitle ("OK to exit?"). Builder.setpositivebutton ("OK", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick ( Dialoginterface dialog, int which) {Showtip ("Log out! ");}}); Builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {Dialog.dismiss ();}}); Builder.create (). Show (); break;} return false;} /** * Initialize ActionBar */private void Initactionbar () {ActionBar ActionBar = Getsupportactionbar (); Actionbar.settitle ("Grab order") ; actionbar.setdisplayhomeasupenabled (true); actionbar.sethomebuttonenabled (true);//Actionbar.seticon ( R.drawable.back_menu);//drawable background = (drawable) getresources ()//. Getdrawable (R.DRAWABLE.TOP_BG);// Getactionbar (). setbackgrounddrawable (background); Settitlecolor (This.getresources (). GetColor (R.color.green));// No response}/** *Show Toast * * @param str */public void Showtip (Final String str) {runonuithread (new Runnable () {@Overridepublic void run () { Mtoast.settext (str); Mtoast.show ();});}
}

id = = Android. R.id.home
The function that is implemented when the icon in the upper left corner of the operation is returned, the configuration checklist needs to be set:

<activity
Android:name= "Com.shop.order.ShopOrderActivity"
Android:launchmode= "Singletop"
Android:theme= "@style/theme.appcompat.light.darkactionbar" >
</activity>

Actionbar_menu's Code:

<?xml version= "1.0" encoding= "Utf-8"? ><menu xmlns:android= "Http://schemas.android.com/apk/res/android"    xmlns:app= "Http://schemas.android.com/apk/res-auto" >        <item        android:id= "@+id/new_order_bar"        android:title= "@string/new_order"        android:icon= "@drawable/ic_launcher"        app:showasaction= "ifroom| Withtext "/>        <item        android:id=" @+id/actionbar_overflow "        android:icon=" @drawable/abc_ic_menu_ Moreoverflow_normal_holo_dark "        android:title=" @string/pop "        app:showasaction=" Ifroom|withtext "/> </menu>

Note : app:showasaction= "Ifroom|withtext" is assumed to be written android:showasaction= "Ifroom|withtext" is not displayed in Actionbar, When you manipulate the menu key in the lower-right corner of your phone


Actionbar_pop's Code:

<?

XML version= "1.0" encoding= "Utf-8"? ><menu xmlns:android= "Http://schemas.android.com/apk/res/android" > <item android:id= "@+id/actionbar_settings" android:title= "@string/settings"/> <item android:id= "@+id/historyorder" android:title= "@string/historyorder"/> <item android:id= "@+id/logout" android:title= "@string/logout"/></menu>

Separating the Action Bar

When the application executes at ANDROID4.0 (API level 14) or above, another additional mode called "Delimited Action Bar" is available for action Bar. When you enable the separate Action Bar mode. A separate bar appears at the bottom of the screen to show all the actions the activity performs on the narrow screen social security.

To enable the detach action bar. Simply add the uioptions= "Splitactionbarwhennarrow" property setting to the <application> or <activity> element.

The left side of the column is not set to a separate action bar. The diagram on the right is a separate action bar:




Use of Android Actionbar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.