Android Actionbar Details (iii): Actionbar implementation Switch Tabs label

Source: Internet
Author: User

Implement switch tabs label ;

Activity code:

public class Actionbartabs extends Activity {
  1. @Override
  2. protected void OnCreate (Bundle savedinstancestate) {
  3. Super.oncreate (savedinstancestate);
  4. Setcontentview (R.layout.action_bar_tabs);
  5. }
  6. Public void Onaddtab (View v) {
  7. Final ActionBar bar = Getactionbar ();
  8. Final int tabcount = Bar.gettabcount ();
  9. Final String Text = "tab" + Tabcount;
  10. Bar.addtab (Bar.newtab (). SetText (text)
  11. . Settablistener (new Tablistener (new Tabcontentfragment (text)));
  12. }
  13. Public void Onremovetab (View v) {
  14. Final ActionBar bar = Getactionbar ();
  15. Bar.removetabat (Bar.gettabcount ()- 1);
  16. }
  17. Public void Ontoggletabs (View v) {
  18. Final ActionBar bar = Getactionbar ();
  19. if (bar.getnavigationmode () = = Actionbar.navigation_mode_tabs) {
  20. Bar.setnavigationmode (Actionbar.navigation_mode_standard);
  21. Bar.setdisplayoptions (Actionbar.display_show_title, actionbar.display_show_title);
  22. } Else {
  23. Bar.setnavigationmode (Actionbar.navigation_mode_tabs);
  24. Bar.setdisplayoptions (0, Actionbar.display_show_title);
  25. }
  26. }
  27. Public void Onremovealltabs (View v) {
  28. Getactionbar (). Removealltabs ();
  29. }
  30. Private class Tablistener implements Actionbar.tablistener {
  31. Private Tabcontentfragment mfragment;
  32. Public Tablistener (Tabcontentfragment fragment) {
  33. Mfragment = fragment;
  34. }
  35. Public void ontabselected (Tab tab, fragmenttransaction ft) {
  36. Ft.add (R.id.fragment_content, Mfragment, Mfragment.gettext ());
  37. }
  38. Public void ontabunselected (Tab tab, fragmenttransaction ft) {
  39. Ft.remove (mfragment);
  40. }
  41. Public void ontabreselected (Tab tab, fragmenttransaction ft) {
  42. Toast.maketext (actionbartabs.   This, "reselected!", Toast.length_short). Show ();
  43. }
  44. }
  45. Private class Tabcontentfragment extends Fragment {
  46. Private String MText;
  47. Public tabcontentfragment (String text) {
  48. MText = text;
  49. }
  50. Public String GetText () {
  51. return mText;
  52. }
  53. @Override
  54. Public View Oncreateview (layoutinflater inflater, ViewGroup container,
  55. Bundle savedinstancestate) {
  56. View Fragview = inflater.inflate (r.layout.action_bar_tab_content, container, false);
  57. TextView Text = (TextView) Fragview.findviewbyid (R.id.text);
  58. Text.settext (MText);
  59. return fragview;
  60. }
  61. }
  62. }


The Action_bar_tabs.xml code for the layout file involved is:

< XML version="1.0" encoding="Utf-8"?>
  1. < LinearLayout xmlns:android="Http://schemas.android.com/apk/res/android"
  2. Android:layout_width="Match_parent"
  3. android:layout_height="Match_parent"
  4. android:orientation="vertical" >
  5. < framelayout android:id="@+id/fragment_content"
  6. Android:layout_width="Match_parent"
  7. android:layout_height="0dip"
  8. android:layout_weight="1"/>
  9. < LinearLayout android:layout_width="Match_parent"
  10. android:layout_height="0dip"
  11. android:layout_weight="1"
  12. android:orientation="vertical" >
  13. < button android:id="@+id/btn_add_tab"
  14. Android:layout_width="Wrap_content"
  15. android:layout_height="Wrap_content"
  16. android:text="@string/btn_add_tab"
  17. android:onclick="Onaddtab"/>
  18. < button android:id="@+id/btn_remove_tab"
  19. Android:layout_width="Wrap_content"
  20. android:layout_height="Wrap_content"
  21. android:text="@string/btn_remove_tab"
  22. android:onclick="Onremovetab"/>
  23. < button android:id="@+id/btn_toggle_tabs"
  24. Android:layout_width="Wrap_content"
  25. android:layout_height="Wrap_content"
  26. android:text="@string/btn_toggle_tabs"
  27. android:onclick="Ontoggletabs"/>
  28. < button android:id="@+id/btn_remove_all_tabs"
  29. Android:layout_width="Wrap_content"
  30. android:layout_height="Wrap_content"
  31. android:text="@string/btn_remove_all_tabs"
  32. android:onclick="Onremovealltabs"/>
  33. </linearlayout>
  34. </linearlayout>


Layout file action_bar_tab_content.xml;

< XML version= "1.0" encoding= "utf-8"?>

    1. < TextView xmlns:android="Http://schemas.android.com/apk/res/android"
    2. android:id="@+id/text"
    3. Android:layout_width="Wrap_content"
    4. android:layout_height="Wrap_content"/>

Android Actionbar Details (iii): Actionbar implementation Switch Tabs label

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.