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

Source: Internet
Author: User
Tags gettext

Implement switch tabs label ;

Activity code:

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


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

 

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


Layout file action_bar_tab_content.xml;

 

      1. < XML version= "1.0" encoding= "utf-8"?>
      2. < TextView xmlns:android="Http://schemas.android.com/apk/res/android"
      3. android:id="@+id/text"
      4. Android:layout_width="Wrap_content"
      5. android:layout_height="Wrap_content"/>
Copy to Google TranslateTranslation Results
the JAVA"In viewplaincopy

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.