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.