Android ActionBar詳解(三):ActionBar實現切換Tabs標籤

來源:互聯網
上載者:User

標籤:android   style   http   io   ar   java   sp   strong   檔案   

實現切換Tabs標籤;

   

 Activity代碼:    

  1. public class ActionBarTabs extends Activity {  
  2.   
  3. @Override  
  4. protected void onCreate(Bundle savedInstanceState) {  
  5. super.onCreate(savedInstanceState);  
  6. setContentView(R.layout.action_bar_tabs);  
  7. }  
  8.   
  9. public void onAddTab(View v) {  
  10. final ActionBar bar = getActionBar();  
  11. final int tabCount = bar.getTabCount();  
  12. final String text = "Tab " + tabCount;  
  13.   
  14. bar.addTab(bar.newTab().setText(text)  
  15. .setTabListener(new TabListener(new TabContentFragment(text))));  
  16. }  
  17.   
  18. public void onRemoveTab(View v) {  
  19. final ActionBar bar = getActionBar();  
  20. bar.removeTabAt(bar.getTabCount() - 1);  
  21. }  
  22.   
  23. public void onToggleTabs(View v) {  
  24. final ActionBar bar = getActionBar();  
  25.   
  26. if (bar.getNavigationMode() == ActionBar.NAVIGATION_MODE_TABS) {  
  27. bar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);  
  28.   
  29. bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE, ActionBar.DISPLAY_SHOW_TITLE);  
  30. } else {  
  31. bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);  
  32. bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);  
  33. }  
  34. }  
  35.   
  36. public void onRemoveAllTabs(View v) {  
  37. getActionBar().removeAllTabs();  
  38. }  
  39.   
  40. private class TabListener implements ActionBar.TabListener {  
  41. private TabContentFragment mFragment;  
  42. public TabListener(TabContentFragment fragment) {  
  43.   
  44. mFragment = fragment;  
  45. }  
  46.   
  47. public void onTabSelected(Tab tab, FragmentTransaction ft) {  
  48. ft.add(R.id.fragment_content, mFragment, mFragment.getText());  
  49. }  
  50.   
  51.    
  52. public void onTabUnselected(Tab tab, FragmentTransaction ft) {  
  53. ft.remove(mFragment);  
  54. }  
  55.   
  56. public void onTabReselected(Tab tab, FragmentTransaction ft) {  
  57. Toast.makeText(ActionBarTabs.this, "Reselected!", Toast.LENGTH_SHORT).show();  
  58. }  
  59.   
  60. }  
  61.   
  62. private class TabContentFragment extends Fragment {  
  63. private String mText;  
  64. public TabContentFragment(String text) {  
  65. mText = text;  
  66. }  
  67.   
  68. public String getText() {  
  69. return mText;  
  70. }  
  71.     
  72. @Override  
  73. public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  74. Bundle savedInstanceState) {  
  75. View fragView = inflater.inflate(R.layout.action_bar_tab_content, container, false);  
  76. TextView text = (TextView) fragView.findViewById(R.id.text);  
  77. text.setText(mText);  
  78. return fragView;  
  79. }  
  80. }  
  81. }  


涉及的布局檔案action_bar_tabs.xml代碼為:

  

  1. < ?xml version="1.0" encoding="utf-8"?>  
  2. < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.   
  4. android:layout_width="match_parent"  
  5. android:layout_height="match_parent"  
  6. android:orientation="vertical">  
  7.   
  8. < FrameLayout android:id="@+id/fragment_content"  
  9. android:layout_width="match_parent"  
  10. android:layout_height="0dip"  
  11. android:layout_weight="1" />  
  12.   
  13. < LinearLayout android:layout_width="match_parent"  
  14. android:layout_height="0dip"  
  15. android:layout_weight="1"  
  16. android:orientation="vertical">  
  17.   
  18. < Button android:id="@+id/btn_add_tab"  
  19. android:layout_width="wrap_content"  
  20. android:layout_height="wrap_content"  
  21. android:text="@string/btn_add_tab"  
  22. android:onClick="onAddTab" />  
  23.   
  24. < Button android:id="@+id/btn_remove_tab"  
  25. android:layout_width="wrap_content"  
  26. android:layout_height="wrap_content"  
  27. android:text="@string/btn_remove_tab"  
  28. android:onClick="onRemoveTab" />  
  29.   
  30. < Button android:id="@+id/btn_toggle_tabs"  
  31. android:layout_width="wrap_content"  
  32. android:layout_height="wrap_content"  
  33. android:text="@string/btn_toggle_tabs"  
  34. android:onClick="onToggleTabs" />  
  35.   
  36. < Button android:id="@+id/btn_remove_all_tabs"  
  37. android:layout_width="wrap_content"  
  38. android:layout_height="wrap_content"  
  39. android:text="@string/btn_remove_all_tabs"  
  40. android:onClick="onRemoveAllTabs" />  
  41. < /LinearLayout>  
  42.   
  43. < /LinearLayout>  


布局檔案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.   
    4. android:id="@+id/text"  
    5. android:layout_width="wrap_content"  
    6. android:layout_height="wrap_content" />
複製去Google翻譯翻譯結果
【JAVA】鑒於plaincopy

Android ActionBar詳解(三):ActionBar實現切換Tabs標籤

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.