Android Program Development: (8) use the activity bar-8.1 to manipulate the ActionBar

Source: Internet
Author: User

In addition to fragments, newly added features in Android3 and 4, as well as ActionBar (activity bar ). The ActionBar is located at the top of the traditional title bar. The ActionBar displays the application icon and Activity title. Optional, the action items (active project) on the right of the ActionBar ). Shows the build-in (built-in) Email application, the application icon, the title of Activity, and some Activity projects. The next section describes action items in detail ).

 

The following example describes how to hide and display the ActionBar by writing code.

1. Create a project named MyActionBar.

2. Press F11 to debug the simulator. At this point, you will see that the ActionBar is placed at the top of the screen (including the application icon and the Application name "MyActionBar "). For example.

 

3. To hide the ActionBar, add the following code to AndroidManifest. xml.

[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "net. learn2develop. MyActionBar"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">
 
<Uses-sdk android: minSdkVersion = "13"/>
 
<Application
Android: icon = "@ drawable/ic_launcher"
Android: label = "@ string/app_name">
<Activity
Android: label = "@ string/app_name"
Android: name = ". MyActionBarActivity"
<! -- Pay attention to this line of code -->
Android: theme = "@ android: style/Theme. Holo. NoActionBar">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
 
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
</Application>
 
</Manifest>
4. Press F11 to re-Debug and you will see that this ActionBar will not be displayed.


5. You can also write code to remove the ActionBar, but this requires the ActionBar class. To do this, you must first remove the android: theme attribute in AndroidManifest. xml. Otherwise, an Exception will occur ).

6. Add the following code to the MyActionBar. java file.

[Java]
Public class MyActionBarActivity extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
// Obtain the ActionBar instance
ActionBar actionBar = getActionBar ();
// Hide
ActionBar. hide ();
// Display
// ActionBar. show ();
}

}
7. Press F11 to re-Debug and find that ActionBa is still hidden. However, the android: theme attribute in AndroidManifest. xml has been deleted.
Note:

If a topic is defined in AndroidManifest. xml, the ActionBar is hidden. When the program runs, if the getActionBar () method is executed, a null value is returned. Therefore, you can control the display and hiding of ActionBar by writing code.

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.