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

    实现切换Tabs标签;

      

     Activity代码:  

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


    涉及的布局文件action_bar_tabs.xml代码为:

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


    布局文件action_bar_tab_content.xml;

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

    1. < TextView xmlns:android="http://schemas.android.com/apk/res/android"  
    2.   
    3. android:id="@+id/text"  
    4. android:layout_width="wrap_content"  
    5. android:layout_height="wrap_content" /> 
  • 相关阅读:
    隐藏窗口任务栏图标
    初始化 键盘设备
    web2.0最全的国外API应用集合
    about hadoop-eclipse-plugin used by IDE
    Instructions Set JAVA_HOME System-Wide
    腾讯面试题 腾讯面试题:给40亿个不重复的unsigned int的整数,没排过序的,然后再给一个数,如何快速判断这个数是否在那40亿个数当中?
    word 2013 没有控件菜单怎么办,添加控件菜单
    面试题:实现LRUCache::Least Recently Used的缩写,意思是最近最少使用,它是一种Cache替换算法
    面试题:m个长度为n的ordered array,求top k 个 数字
    面试题: generate an equation, by inserting operator add ("+") and minus ("-") among the array to make equationExpression == 0
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/4096731.html
Copyright © 2011-2022 走看看