Android Actionbar (i): Actionbar Overview and its creation

Source: Internet
Author: User

In Android 3.0, in addition to our focus on the fragment, Action Bar is also an important content, action Bar is mainly used to replace the traditional title bar, for Android tablet device, the screen is larger its title use Action Bar to design to show more rich content, easy to control.

  The main functions of the Action bar include:

1. Display the Options menu
2. Provide the navigation function of the switch mode of the tab page, can switch multiple fragment.
3. Provide a drop-down navigation entry.
4. Provide interactive activity view instead of option entry
5. Use the program's icon as the back home screen or up navigation action.

Tips for applying Actionbar in your program there are a few points to note, the SDK and the final running firmware must be Android 3.0, or honeycomb, to add Android to the USES-SDK element in the Androidmanifest.xml file: Minsdkversion or android:targetsdkversion, similar

    1. < manifest xmlns:android="Http://schemas.android.com/apk/res/android"
    2. package="EOE.ANDROID.CWJ"
    3. android:versioncode="1"
    4. Android:versionname="1.0" >
    5. < USES-SDK android:minsdkversion="Honeycomb"/>
    6. < Application >
    7. </application>
    8. </manifest>

If you need to hide the action bar to set the theme style for Notitlebar in your activity's properties in your manifest file, the following code hides the title before 3.0, and after 3.0 it hides Actionbar, and the code is:

 

    1. < activity android:theme="@android: Style/theme.notitlebar" >


First, add the active item action items

For the active item, you can see the right part of the title of Android 3.0 as a toolbar, and the following save and delete are the two action items activity entries.

Below is a menu layout file code

 

    1. < XML version= "1.0" encoding= "utf-8"?>
    2. < menu xmlns:android="Http://schemas.android.com/apk/res/android" >
    3. < item android:id="@+id/menu_add"
    4. android:icon="@drawable/ic_menu_save"
    5. android:title="@string/menu_save"
    6. android:showasaction="Ifroom|withtext"/>
    7. </menu>


Other code is similar to the menu in activity, such as

  1. @Override
  2. Public Boolean onoptionsitemselected (MenuItem item) {
  3. Switch (Item.getitemid ()) {
  4. Case Android. R.id.home:
  5. Executes the following intent when the icon of the action Bar is clicked
  6. Intent Intent = new Intent (This, Android123.   Class);
  7. StartActivity (Intent);
  8. Break
  9. }
  10. Return super.onoptionsitemselected (item);
  11. }


For the creation of Actionbar, you can override the OnStart method in your activity:

 

    1. @Override
    2. protected void OnStart () {
    3. Super.onstart ();
    4. ActionBar ActionBar = This.getactionbar ();
    5. Actionbar.setdisplayoptions (actionbar.display_home_as_up, actionbar.display_home_as_up);
    6. }


You need to be aware that you must call Getactionbar in the oncreate of your activity when you call Setcontentview.

  Second, add the active view action

For Actionview, we can customize the Searchview layout in the menu layout file using, as follows:

 

    1. < item android:id="@+id/menu_search"
    2. android:title="Search"
    3. android:icon="@drawable/ic_menu_search"
    4. android:showasaction="Ifroom"
    5. android:actionlayout="@layout/searchview"/>


You can also specify the Searchview control in the Android system directly, then the menu "_search" code to write this way:

    1. < item android:id="@+id/menu_search"
    2. android:title="Search"
    3. android:icon="@drawable/ic_menu_search"
    4. android:showasaction="Ifroom"
    5. android:actionviewclass="Android.widget.SearchView"/>


It is important to note that one of the two methods above is actionlayout to develop an XML layout file, one that specifies a class Actionviewclass The final call can map the menu layout in response to the Oncreateoptionsmenu method in activity.

 

      1. @Override
      2. Public Boolean Oncreateoptionsmenu (Menu menu) {
      3. Getmenuinflater (). Inflate (r.menu.options, menu);
      4. Searchview Searchview = (searchview) menu.finditem (r.id.menu_search). Getactionview ();
      5. Return Super.oncreateoptionsmenu (menu);
      6. }
Copy to Google TranslateTranslation Results
the JAVA"In viewplaincopy

Android Actionbar (i): Actionbar Overview and its creation

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.