Learn ActionBar

1, add ActionBar

ActionBar的添加非常简单,只需要在AndroidManifest.xml中指定Application或Activity的theme是Theme.Holo或其子类就可以了,在Android 3.0及更高的版本中,Activity中都默认包含有ActionBar组件。

2, cancel ActionBar

If you need to hide the Action Bar can set a theme in the style of your property Activity for NoTitleBar in your manifest file

  Another approach, calling hide at runtime () method can also hide the ActionBar, call the show () method to display ActionBar ().

ActionBar = getActionBar ActionBar ();
actionBar.hide ();
  When you hide the ActionBar, the entire contents of the system will fill the entire space of Activity.

Note: If you are using a theme (theme) was to remove the ActionBar Activity, then the window will no longer be ActionBar, so there is no way at run-time to add ActionBar-- call getActionBar () method returns a null value.

3. Modify the Action Bar icon and title

By default, the system will use or the icon attribute specifies the picture as ActionBar icons, but we can change this default behavior. If we want to use another photo as ActionBar icons can be specified with the logo or property, the title and content of the label property to specify. For example, there is a cnblog_icon.png res picture under the project / drawable directory, you can specify this in AndroidManifest.xml:


The effect is as follows:

4. Add Action button

ActionBar may also be provided associated with the current functions of the application Action buttons that are displayed directly on the ActionBar icons or text form. Of course, if too many buttons, can not be displayed on the ActionBar, some buttons extra overflow can be hidden inside (three rightmost point is the overflow button), click overflow button to see all the Action button.

When the Activity starts, the system will call onCreateOptionsMenu Activity of () method to remove all of the Action buttons, we only need this method to load a menu resource, and all the Action buttons are defined in the resource file inside it .

Published 79 original articles · won praise 16 · views 20000 +

Guess you like

Origin blog.csdn.net/weixin_42446445/article/details/104842822