ActionBar uses the application icon to respond to the operation

Source: Internet
Author: User

ActionBar uses the application icon to respond to the operation
Call the setDisplayHomeAsUpEnabled () method of the ActionBar class to use the application icon as the navigation bar.
The arrow to the left is automatically added.
Use other icons:
Call the setIcon () method of the ActionBar class.


// Obtain the ActionBar object
ActionBar actionbar = getActionBar ();
// Set the icon
ActionBar. setIcon (R. drawable. ic_launcher );
// Use the icon as the navigation bar
ActionBar. setDisplayHomeAsUpEnabled (true)


The resource ID of this location is android. R. id. home,
Developers can respond to click events with this ID.
Override onMenuItemSelected (or onOptionsItemSelected () method in Activity,
You can respond to click events at this location. The common response method is to return the previous interface or the main interface, using INtent

Activate other Activity components, or call finish () to end the current Activity (when the current Activity ends, the previous Activity returns to the foreground ).

Directly run the Code:

Package com. example. actionbar_test; import android. OS. bundle; import android. annotation. suppressLint; import android. app. actionBar; import android. app. activity; import android. content. intent; import android. view. menu; import android. view. menuItem; public class ActionBar_Activity extends Activity {@ SuppressLint ("NewApi") @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_action_bar _); ActionBar actionbar = getActionBar (); actionbar. setIcon (R. drawable. home_checked); actionbar. setDisplayHomeAsUpEnabled (true) ;}@ Overridepublic boolean onOptionsItemSelected (MenuItem item) {// TODO Auto-generated method stubint id = item. getItemId (); if (id = R. id. action_settings) {return true;} if (id = android. r. id. home) {// click Intent intent = new Intent (this, MainActivity. class); startActivity (intent); finish ();} return super. onOptionsItemSelected (item );}}

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.