zoukankan      html  css  js  c++  java
  • Android ActionBar详解(三):ActionBar实现切换Tabs标签

    实现切换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" />

    【JAVA鉴于plaincopy
  • 相关阅读:
    摄像机Rtsp地址格式大全
    Html5 播放Hls格式视频
    Libx264 编码错误 Input picture width(320) is greater than stride (0)
    xtrabackup: error: last checkpoint LSN (3409281307) is larger than last copied LSN (3409274368). #2
    通过FEDERATED存储引擎同步两实例间的表数据
    MySQL将内存用在了哪里
    通过performance schema收集慢查询
    记一次定时备份任务的失败原因
    mysqld got signal 11
    修改mysql数据的字符集校验规则使其区分大小写
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/4091868.html
Copyright © 2011-2022 走看看