Android ActionBar (Official Guide)

Source: Internet
Author: User

Install ActionBar:

Support Android 3.0 and Above Only (Only Versions later than 3.0 are supported)

      
       ...
  

Support Android 2.1 and Above (if you want applications to Support version 3.1, You need to perform the following operations)
1. Install the v7 appcompat Library
2 Update your activity so that it extends ActionBarActivity. For example:
public class MainActivity extends ActionBarActivity { ... }
3In your manifest file, update either the element or individual elements to use one of the Theme. AppCompat themes. For example:
 

Adding Action Buttons (add ActionBar ):
1. Specify the Actions in XML

Version 3.0 or later:

      
       
       
       
   
  


Version 2.0 or later:

      
       
       ...
  

2. Add the Actions to the Action Bar: Make ActionBar take effect

@Overridepublic boolean onCreateOptionsMenu(Menu menu) {    // Inflate the menu items for use in the action bar    MenuInflater inflater = getMenuInflater();    inflater.inflate(R.menu.main_activity_actions, menu);    return super.onCreateOptionsMenu(menu);}

3. Respond to Action Buttons corresponding ActionBar event

@Overridepublic boolean onOptionsItemSelected(MenuItem item) {    // Handle presses on the action bar items    switch (item.getItemId()) {        case R.id.action_search:            openSearch();            return true;        case R.id.action_settings:            openSettings();            return true;        default:            return super.onOptionsItemSelected(item);    }}

Add Up Button for Low-level Activities: Add the action Button for returning to the previous Activity:
1. Whether it is version 4.0 or later or use ActionBarActivity to support databases:

Added the android: parentActivityName attribute.

    ...    
              ...        
              
          
      

Call setDisplayHomeAsUpEnabled ():
@Overridepublic void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_displaymessage);    getSupportActionBar().setDisplayHomeAsUpEnabled(true);    // If your minSdkVersion is 11 or higher, instead use:    // getActionBar().setDisplayHomeAsUpEnabled(true);}

Because the android: parentActivityName attribute is set, the system knows the parent Activity to be returned, so you do not need to set event listening.


Styling the Action Bar (set the ActionBar style ):
Use an Android Theme (when using the supported libraries, they are as follows, corresponding to the unsupported libraries in brackets)
When using the Support Library, you must instead use the Theme. AppCompat themes:
Theme. AppCompat for the "dark" theme. (Theme. Holo)
Theme. AppCompat. Light for the "light" theme. (Theme. Holo. Light)
Theme. AppCompat. Light. DarkActionBar for the light theme with a dark action bar. (Theme. Holo. Light. DarkActionBar)

Customize the Background (change the Background of the ActionBar)

For Android 3.0 and higher only:

1. Define the resource file: res/values/themes. xml

                        
  
      
       
       
       
   
  

2. Then apply your theme to your entire app or individual activities (reference resource file ):

For Android 2.1 and higher:1.res/values/themes.xml:                        
      
       
       
       
   
  

2. Then apply your theme to your entire app or individual activities:
 
 

Customize the Text Color (custom ActionBar font Color)
For Android 3.0 and higher only:

Res/values/themes. xml:

  
      
       
       
       
       
       
       
       
   
  

For Android 2.1 and higher (When using the Support Library, your style XML file might look like this :):

Res/values/themes. xml

                        
  
      
       
       
       
       
       
       
       
   
  


Customize the Tab Indicator (custom Tab TabHost style, using selector)

Res/drawable/actionbar_tab_indicator.xml:

                
  
  
       
       
       
       
       
       
   
       
       
       
       
       
       
   
  

Then:
For Android 3.0 and higher only:

Res/values/themes. xml:

  
      
       
       
       
   
  

For Android 2.1 and higher:

Res/values/themes. xml:

  
      
       
       
       
   
  

Overlaying the Action Bar (automatically hiding the ActionBar ):
Enable Overlay Mode (Auto Hide Mode ):

For Android 3.0 and higher only:

                        
      
       
   
  

Then apply your theme to your entire app or individual activities:
 
 

For Android 2.1 and higher:

                        
      
       
   
  

Then apply your theme to your entire app or individual activities:
 
 

Specify Layout Top-margin (top left blank to avoid contact with ActionBar ):

No supported libraries are used:

                
  
       ...
  

Supported libraries are used:

                
  
  
       ...
  


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.