How low-version Android uses Actionbar

Source: Internet
Author: User

The following example is api<11, because there is a actionbar that can be used when api>=11, so don't guess the scope of the discussion


Today, Google released the latest API 18, including a lot of new performance, just recently in the study of Actionbarsherlock, see the latest support V7 package also began supporting Actionbar, studied the use of methods, I have a study of the use of the method I have to record the following, I hope it can be a catalyst.
1. Import the support V7 package. After you update SDK 18, you will find three subfolders in the \ANDROID-SDK\EXTRAS\ANDROID\SUPPORT\V7 directory, respectively: appcompat\gridlayout\ Mediarouter, the work is more commonly used is appcompat, so today also only looked at the Android-support-v7-appcompat.jar under this directory. Import this file into the project.
2. In the manifest file, add the Theme attribute for the actitity you used Actionbar, this Theme must be @style/theme.appcompat, @style/ Theme.AppCompat.Light., @style/theme.appcompat.light.darkactionbar, three kinds of one, I lazy, directly in the application node added Theme.

3. In the Res/menu directory, write an XML file. The official recommendation is to have the icon and title icons for each item item, because the default display is the icon, and when the screen space is not enough, the menu that is not displayed is hidden in the list, and only the value of the Title field is displayed in the list.

    Android:id= "@+id/action_search"    android:icon= "@drawable/left"    android:title= "search"/>    android: Id= "@+id/action_compose"    android:icon= "@drawable/right" android:title= "Compose"/>    

4. Implement the activity and let it inherit actionbaractivity. In the Oncreateoptionsmenu () method, get to each of the MenuItem, The Menuitemcompat.setshowasaction (MenuItem item, int actionenum) method is then called for each MenuItem to change the menu item to a subkey in Actionbar.
The actionenum values are as follows:
Menuitemcompat.show_as_action_always (always shown)
Menuitemcompat.show_as_action_collapse_action_view (shown in hidden list)
Menuitemcompat.show_as_action_if_room (screen has space on display, no space to hide)
Menuitemcompat.show_as_action_never (never show)
Menuitemcompat.show_as_action_with_text (displays both icons and text)
Finally in the method to return True


<pre name= "code" class= "Java" >public boolean oncreateoptionsmenu (Menu menu) {menuinflater Menuinflater = Getmenui  Nflater ();  Menuinflater.inflate (R.menu.main, menu);  MenuItem MenuItem = Menu.finditem (R.id.action_search);  MenuItem FindItem = Menu.finditem (r.id.action_compose);  Menuitemcompat.setshowasaction (MenuItem,      menuitemcompat.show_as_action_always);  Menuitemcompat.setshowasaction (FindItem,      menuitemcompat.show_as_action_with_text); return true;}

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.