Use of Action Bar in Android

Source: Internet
Author: User

Use of Action Bar in Android
Content Overview

Demo and Basic Introduction
Enable Action Bar
Add button on Action Bar
Custom Action Bar style
Hide Action Bar automatically
Use of Action Provider
Use of ActionBarSherlock

Demo and Basic Introduction

There are two scenarios for using ActionBar:
1. Action Bar on devices BEFORE Android 3.0 (API 11)
1) ActionBarSherlock open source Library
2) ActionBarCompat library from the Android support library v7
2. Action Bar on devices AFTER Android 3.0 (API 11)

Before Android, you need to use Action Bar in the above two ways. The first method is to use the ActionBarSherlock open source Library (Is an extension of the android compatibility development kit.If the application runs on a device above Android, the native Action Bar is used by default. Otherwise, the Action Bar provided by the Open Source library is used. The second method is to use the open-source support v7 library officially provided by google.
The native Action Bar can be used on devices after android3.0

Enable Action Bar

1. Action Bar on devices AFTER Android 3.0 (API 11)
Starting from Android 3.0, the Action bar is included in the Activity with the Theme. Holo topic used (Or a subclass of Theme. Holo.) Theme. Holo is the default topic. When both minSdkVersion and targetSdkVersion are 11 or later.
2. Action Bar on devices BEFORE Android 3.0 (API 11)
Must be a device of more than 2.1, You need to include the Support Library (android-support-v7-appcompat) operation steps in the application:
1) import the android-sdk-windows \ extras \ android \ support \ v7 \ appcompat library, copy it to the workspace, and finally introduce it to your project.
2) inherit activity from ActivityBarActivity
3) change the topic in the AndroidManifest. xml file
Android: theme = "@ style/Theme. AppCompat. Light. DarkActionBar"

Add button on Action Bar

1. For Android 3.0 and higher only
The Add button is divided into two parts. The first part specifies the buttons in xml (Add different item items to menu)


  

Attribute Introduction
1) android: showAsAction = "always"
Always displayed on Action Bar
2) android: showAsAction = "ifRoom"
If the Action Bar has enough space
3) android: showAsAction = "never"
Not Displayed in Action Bar, collapsed in OverFlow
4) android: showAsAction = "withText"
The menu item is displayed together with its icon and menu text

2. For Android 2.1 and higher
1) Add a custom namespace to menu. xml
Xmlns: yourapp = "http://schemas.android.com/apk/res-auto"

2) Specify the namespace yourapp: showAsAction = "ifRoom" before the showAsAction attribute"

Add Click Event on the Action Bar

@ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true;} // Add a click event @ Override public boolean onOptionsItemSelected (MenuItem item) {switch (item. getItemId () {case R. id. action_search: Toast. makeText (this, "Action_Search", 0 ). show (); break; case R. id. action_settings: Toast. makeText (this, "Action_Settings", 0 ). show (); break;} return super. onOptionsItemSelected (item );}

Add the icon in the upper left corner of the ActionBarUpOrReturnButton (View google's official documentation.

// Enable the up button getSupportActionBar (). setDisplayHomeAsUpEnabled (true); // getActionBar (). setDisplayHomeAsUpEnabled (true); // above 3.0

         
                
         
                           
           
    
   
  

Note: If the parentActivityName attribute is greater than 3.0, use "meta-data" for less than 3.0"

Custom Action Bar style

Use themes provided by android

Theme. Holo for "dark" theme Theme. Holo. Light for a "light" theme

For example


  

The above is the theme that comes with Android 3.0 and later. For android 2.1 and later devices, use the following method:

For example


  

Customize the Background (the Background color of the custom Action Bar)
For Android 3.0 and higher only
1. Create a custom Style in themes. xml to inherit the existing Action Bar Style (such as Theme. Holo)
2. Override the actionBarStyle attribute.
3. The actionBarStyle attribute value points to another Style that has been overwritten with the background attribute.
4. Specify the property value of the background.

Themes. xml


  
  
   
   
  

Reference a custom topic in AndroidManifest. xml

android:theme="@style/CustomActionBarTheme"

For Android 2.1 and higher

Themes. xml


  
  
   
   
  
  

Finally, introduce the custom topic in the configuration file.

Customize the Color reference official documentation

Customize the Tab Indicator

ActionBar actionBar = getActionBar (); // for <3.0 getSupportActionBar (); actionBar. setNavigationMode (ActionBar. NAVIGATION_MODE_TABS); // sets the navigation mode, ActionBar. NAVIGATION_MODE_LIST ActionBar. tabListener tabLiatener = new ActionBar. tabListener () {@ Override // public void onTabUnselected (Tab tab, FragmentTransaction ft) When not selected) {// TODO Auto-generated method stub} @ Override // public void onTabSelected (Tab tab, FragmentTransaction ft) {// TODO Auto-generated method stub Toast. makeText (MainActivity. this, "TabSelected" + tab. getPosition (), 0 ). show () ;}@ Override // public void onTabReselected (Tab tab, FragmentTransaction ft) {// TODO Auto-generated method stub }}; // Add three labels for (int I = 0; I <3; I ++) {Tab = actionBar. newTab (); tab. setText ("Tab" + I); tab. setTabListener (tabLiatener); actionBar. addTab (tab); // Add to actionbar}

Customize the style of a Tab (Refer to official documents) You can also generate the Action Bar Tab style with one click. After setting the style, download and decompress the file directly and copy all the files in the res directory to the project, finally, use the style generated by one-click Project in the configuration file.(You can go to Google's official documentation to learn more about how to use custom styles)

Automatically hide the Action Bar (Overlaying the Action Bar)

By default, the Action Bar appears at the top of the current activity, reducing the remaining space available in the Activity layout. In this case, you can call hide () and show () to hide or display the Action Bar. However, this method will make your Activity calculate and re-draw the layout based on the new size, to avoid re-painting, you can enable the overlay mode of the action bar, in overlay mode, the activity can use the available space and draw the Action bar to the front of the current layout instead of the top of the preceding figure, in this way, when the action bar is hidden or displayed, the layout size is not calculated previously.

Enable Overlay Mode (Enable overlay Mode)
You need to create a custom topic. This topic needs to expand an existing Action bar topic and set the android: windowActionBarOverly attribute to true,(More than 3.0 of devices and more than 2.1 of devices are also divided into two parts)
1. For Android 3.0 and higher only

2. For Android 2.1 and higher

Specify Layout Top-margin (margintop of the specified Layout)

Why do you need to know marginTop? When the Action Bar is in overlay mode
It occupies some visible la S. To ensure that these la s are kept in the lower part of the Action bar, you can add the marginTop or paddingTop attribute to it and specify the attribute value as actionBarSize.

For example

1. For Android 3.0 and higher only


  
       ...
  

2. For Android 2.1 and higher


  
       ...
  
Use of Action Provider

Knowledge Point (XML)

1. For Android 3.0 and higher only <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxwcmUgY2xhc3M9 "brush: java;">

2. For Android 2.1 and highers


  

3.0 For example


  

Knowledge Point (JAVA)

1. For Android 3.0 and higher only

     shareItem = menu.findItem(R.id.action_share);     shareItem.getActionProvider();

2. For Android 2.1 and higher

     shareItem = menu.findItem(R.id.action_share);     MenuItemCompat.getActionProvider(shareItem);
Use of ActionBarSherlock

ActionBarSherlock is an extension compatible with the development kit.
Open source library address
Https://github.com/JakeWharton/ActionBarSherlock
Official Website address
Http://actionbarsherlock.com/

Download it, copy actionabarabsherlock to the workspace, and introduce it to the project.
Problems that may occur after import:
Android-support-v4.jar package version inconsistent causes errors...

Knowledge Point
1. android: Theme. Holo --> Theme. Sherlock
Android: Theme. Holo. Light --> Theme. Sherlock. Light
Android: Theme. Holo. Light. DarkActionBar --> Theme. Sherlock. Light. DarkActionBar
.......
2. Activity --> SherlockActivity
ListActivity --> SherlockListActivity
Fragment --> SherlockFragment
.......
3. getActivity () --> getSherlockActivity ()
GetActionBar () --> getSupportActionBar ();
.......

Sample Code

Import com. actionbarsherlock. app. actionBar; import com. actionbarsherlock. app. actionBar. onNavigationListener; import com. actionbarsherlock. app. sherlockActivity; public class MainActivity extends SherlockActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); ActionBar actinBar = getSupportActionBar (); actinBar. setNavigationMode (ActionBar. NAVIGATION_MODE_LIST); // set it to the navigation mode of the list item // Add the ArrayAdapter content for the list item
  
   
Adapter = new ArrayAdapter
   
    
(This, android. r. layout. simple_list_item_1); for (int I = 0; I <5; I ++) {adapter. add ("list item" + I);} actinBar. setListNavigationCallbacks (adapter, new OnNavigationListener () {@ Override public boolean onNavigationItemSelected (int itemPosition, long itemId) {Toast. makeText (MainActivity. this, "select" + itemPosition, 0 ). show (); return false ;}});}}
   
  
 android:theme="@style/Theme.Sherlock.Light.DarkActionBar" 

For more information about ActionBarSherlock settings, visit the official website.

Related Article

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.