Basic usage of Android--actionbar

Source: Internet
Author: User

A description
Android 3.0 appears after 3.0 called title bar display position on the title bar
You can display the app's icon and activity's title
Created in the same way as the system menu, the difference is: android:showasaction= ""
Ifroom display when there is room in the Actionbar
Always shows up in Actionbar
Never is never shown in Actionbar.
Withtext display text
Collapseactionview can be folded

Second, the functions provided in Actionbar
1, menu item showing the options menus
2, can make the application's icon to return the function of the upper level interface
3, provides interactive view and action view
4, you can implement tab-based navigation, you can switch fragment
5, drop-down navigation
6, the use of Actionprovider
Third, how to create
1, create each menu item declaration in the Res/menu file <item/>
Display mode: showasaction
Icons: Icon
2, in activity, overrides the parent class's Oncreateoptionsmenu (Menu menu), loading the menus resource
3, in activity, overrides the Onoptionsitemselected (MenuItem item) of the parent class to handle the click event of each item

Four, split the action bar API 14 or more
Add properties in the manifest file <application/> or <activity/>: android:uioptions= "Splitactionbarwhennarrow"
Five. Start the navigation icon
Function: You can make the icon of the current app as a clickable icon
object to get Actionbar: Getactionbar ()
Setdisplayshowhomeenabled setting whether to display an icon for an application
The icon for the Setdisplayhomeasupenabled settings application can be clicked, and a left arrow appears to the left of the icon
Sethomebuttonenabled Settings app icon can be clicked, but no arrows
Get the application navigation icon id:android. R.id.home

Six, the common method in Actionbar
Actionbar.isshowing () to determine whether the current ActionBar is being displayed
Actionbar.show () Show ActionBar
Actionbar.hide () Hide ActionBar
Seven. Use of Action View
You can edit action items, such as the Searchview control, which can be displayed directly in the Actionbar
There are two ways of implementing this:
1, the Actionviewclass attribute implements the Collapsibleactionview class
2, the Actionlayout property displays the normal layout page
Eight, ActionBar tab navigation function through the Option tab to switch fragment
ActionBar + Fragment
1, gets the Actionbar object, and sets the navigation mode to tabs
Setnavigationmode (int mode) Set Navigation mode
Navigation_mode_standard default mode
Navigation_mode_list list mode
Navigation_mode_tabstab Tag Mode
2, let the current class implement Interface Tablistener, rewrite 3 methods
3, create each tab item and add it to Actionbar
Actionbar.tab Tab = Actionbar.newtab ();
Tab.seticon ();//Set icon
Tab.sttext ();//Set the text to be displayed
Tab.settablistener ();//Set monitoring

Actionbar.add (tab);//Add tab to ActionBar

IX: Removal of Actionbbar

Before the Setcontentview requestwindowfeature (window.feature_no_title);
Or: Android:theme= "@android: Style/theme.black.notitlebar"

To implement a fragment switch instance:

[Java]View PlainCopyprint?
  1. protected void OnCreate (Bundle savedinstancestate) {
  2. super.oncreate (savedinstancestate);
  3. Setcontentview (R.layout.activity_main);
  4. ActionBar = Getactionbar ();
  5. //Get navigation mode
  6. Actionbar.setnavigationmode (Actionbar.navigation_mode_tabs);
  7. //Create Actionbar.tab item and add it to Actionbar
  8. Actionbar.tab Tab = Actionbar.newtab ();
  9. Tab.settext ("headline");
  10. Tab.seticon (R.drawable.ic_launcher);
  11. Tab.settablistener (this);
  12. Actionbar.addtab (tab);
  13. tab = Actionbar.newtab ();
  14. Tab.settext ("Sport");
  15. Tab.seticon (R.drawable.ic_launcher);
  16. Tab.settablistener (this);
  17. Actionbar.addtab (tab);
  18. Actionbar.addtab (Actionbar.newtab (). SetText ("Finance")
  19. . SetIcon (R.drawable.ic_launcher). Settablistener (this));
  20. Actionbar.addtab (Actionbar.newtab (). SetText ("car")
  21. . SetIcon (R.drawable.ic_launcher). Settablistener (this));
  22. }
  23. @Override
  24. public void ontabselected (Tab tab, fragmenttransaction ft) {
  25. //TODO auto-generated method stub
  26. Fragmentnew f = new Fragmentnew ();
  27. Bundle B = new bundle ();
  28. int position = Tab.getposition ();
  29. B.putint ("position", position);
  30. F.setarguments (b);
  31. Ft.replace (R.ID.F, F);
  32. }
  33. @Override
  34. public void ontabunselected (Tab tab, fragmenttransaction ft) {
  35. //TODO auto-generated method stub
  36. }
  37. @Override
  38. public void ontabreselected (Tab tab, fragmenttransaction ft) {
  39. //TODO auto-generated method stub
  40. }
  41. }
[Java]View PlainCopyprint?
  1. Public class Fragmentnew extends Fragment {
  2. @Override
  3. Public View Oncreateview (layoutinflater inflater, ViewGroup container,
  4. Bundle savedinstancestate) {
  5. //TODO auto-generated method stub
  6. TextView t = new TextView (Getactivity ());
  7. int position = Getarguments (). GetInt ("position");
  8. switch (position) {
  9. Case 0:
  10. T.settext ("headline");
  11. Break ;
  12. case 1:
  13. T.settext ("Sport");
  14. Break ;
  15. Case 2:
  16. T.settext ("Finance");
  17. Break ;
  18. Case 3:
  19. T.settext ("car");
  20. Break ;
  21. }
  22. return t;
  23. }
  24. }

[HTML]View PlainCopyprint?
    1. <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
    2. xmlns:tools="Http://schemas.android.com/tools"
    3. android:layout_width="match_parent"
    4. android:layout_height="match_parent"
    5. android:paddingbottom="@dimen/activity_vertical_margin"
    6. android:paddingleft="@dimen/activity_horizontal_margin"
    7. android:paddingright="@dimen/activity_horizontal_margin"
    8. android:paddingtop="@dimen/activity_vertical_margin"
    9. tools:context=". Mainactivity " >
    10. <framelayout
    11. android:id="@+id/f"
    12. android:layout_width="match_parent"
    13. android:layout_height="match_parent" />
    14. </relativelayout>

Basic usage of Android--actionbar

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.