Android Tab導航菜單欄--FragmentTabHost+Fragment實現(轉載)


      FragmentTabHost繼承TabHost,增加了對Fragment的支持。

      Fragment是從Android3.0才引入,Fragment有自己單獨的生命週期,在Activity運行的時候可以很方便的使用。
這裏只是介紹FragmentTabHost的用法。
 

      1.  先看看谷歌官方文檔上的兩個例子:

  1. import com.example.android.supportv4.R;  
  2.   
  3. import android.os.Bundle;  
  4. import android.support.v4.app.FragmentActivity;  
  5. import android.support.v4.app.FragmentTabHost;  
  6.   
  7. /** 
  8.  * This demonstrates how you can implement switching between the tabs of a 
  9.  * TabHost through fragments, using FragmentTabHost. 
  10.  */  
  11. public class FragmentTabs extends FragmentActivity {  
  12.     private FragmentTabHost mTabHost;  
  13.   
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.   
  18.         setContentView(R.layout.fragment_tabs);  
  19.         mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);  
  20.         mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);  
  21.   
  22.         mTabHost.addTab(mTabHost.newTabSpec(”simple”).setIndicator(“Simple”),  
  23.                 FragmentStackSupport.CountingFragment.classnull);  
  24.         mTabHost.addTab(mTabHost.newTabSpec(”contacts”).setIndicator(“Contacts”),  
  25.                 LoaderCursorSupport.CursorLoaderListFragment.classnull);  
  26.         mTabHost.addTab(mTabHost.newTabSpec(”custom”).setIndicator(“Custom”),  
  27.                 LoaderCustomSupport.AppListFragment.classnull);  
  28.         mTabHost.addTab(mTabHost.newTabSpec(”throttle”).setIndicator(“Throttle”),  
  29.                 LoaderThrottleSupport.ThrottledLoaderListFragment.classnull);  
  30.     }  
  31. }  
import com.example.android.supportv4.R;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;

/**
 * This demonstrates how you can implement switching between the tabs of a
 * TabHost through fragments, using FragmentTabHost.
 */
public class FragmentTabs extends FragmentActivity {
    private FragmentTabHost mTabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.fragment_tabs);
        mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

        mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
                FragmentStackSupport.CountingFragment.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),
                LoaderCursorSupport.CursorLoaderListFragment.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("custom").setIndicator("Custom"),
                LoaderCustomSupport.AppListFragment.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("throttle").setIndicator("Throttle"),
                LoaderThrottleSupport.ThrottledLoaderListFragment.class, null);
    }
}

  1. import com.example.android.supportv4.R;  
  2.   
  3. import android.os.Bundle;  
  4. import android.support.v4.app.Fragment;  
  5. import android.support.v4.app.FragmentTabHost;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9.   
  10. public class FragmentTabsFragmentSupport extends Fragment {  
  11.     private FragmentTabHost mTabHost;  
  12.   
  13.     @Override  
  14.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  15.             Bundle savedInstanceState) {  
  16.         mTabHost = new FragmentTabHost(getActivity());  
  17.         mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.fragment1);  
  18.   
  19.         mTabHost.addTab(mTabHost.newTabSpec(”simple”).setIndicator(“Simple”),  
  20.                 FragmentStackSupport.CountingFragment.classnull);  
  21.         mTabHost.addTab(mTabHost.newTabSpec(”contacts”).setIndicator(“Contacts”),  
  22.                 LoaderCursorSupport.CursorLoaderListFragment.classnull);  
  23.         mTabHost.addTab(mTabHost.newTabSpec(”custom”).setIndicator(“Custom”),  
  24.                 LoaderCustomSupport.AppListFragment.classnull);  
  25.         mTabHost.addTab(mTabHost.newTabSpec(”throttle”).setIndicator(“Throttle”),  
  26.                 LoaderThrottleSupport.ThrottledLoaderListFragment.classnull);  
  27.   
  28.         return mTabHost;  
  29.     }  
  30.   
  31.     @Override  
  32.     public void onDestroyView() {  
  33.         super.onDestroyView();  
  34.         mTabHost = null;  
  35.     }  
  36. }  
import com.example.android.supportv4.R;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentTabsFragmentSupport extends Fragment {
    private FragmentTabHost mTabHost;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        mTabHost = new FragmentTabHost(getActivity());
        mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.fragment1);

        mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
                FragmentStackSupport.CountingFragment.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),
                LoaderCursorSupport.CursorLoaderListFragment.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("custom").setIndicator("Custom"),
                LoaderCustomSupport.AppListFragment.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("throttle").setIndicator("Throttle"),
                LoaderThrottleSupport.ThrottledLoaderListFragment.class, null);

        return mTabHost;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mTabHost = null;
    }
}


      第一個例子是在Activity中使用,直接在XML文件中定義,通過setup(Context, FragmentManager, int) 完成TabHost的初始化,再依次添加Tab。

      第二個例子是在Fragment中使用,通過構造函數 mTabHost = new FragmentTabHost(getActivity())和setup(Context, FragmentManager, int) 完成TabHost的初始化,再依次添加Tab。

     注意:

     getFragmentManager拿到的是activity對所包含fragment的Manager;

     getChildFragmentManager()拿到fragment嵌套fragment的Manager;

     引用Android-support-v4的話,要使用 getSupportFragmentManager();


     2.  FragmentTabHost常見用法:


     2.1 如果是在XML文件中定義,通過 mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost)拿到FragmentTabHost,
也可以像第二個例子中使用構造函數拿到FragmentTabHost     

     再看一下FragmentTabHost的構造函數:
     FragmentTabHost(Context context)  
     FragmentTabHost(Context context, AttributeSet attrs) 
     直接使用接行了。   
  

   2.2 FragmentTabHost做初始化的操作

     public void setup () 
     public void setup (Context context, FragmentManager manager) 
     public void setup (Context context, FragmentManager manager, int containerId) args)Bundle  

     2.3 添加tabSpec標籤和Fragment類

      addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) 

     2.4 設置Tab樣式,三個方法

     setIndicator(CharSequence label);第一種爲系統樣式,使用要文字標識tab
     setIndicator(CharSequence label, Drawable icon);第二種可以使用文字+icon標識tab      
     setIndicator(View view) 第三種爲tab自定義View樣式,可以這樣使用:

   View v = getLayoutInflater().inflate( R.layout.tab_main_first, null);
 //設置自定義Tab視圖  
  TabSpec spec1 = mTabHost.newTabSpec(TabTag[i]).setIndicator(v);  
//將Tab按鈕添加進Tab選項卡中  
 mTabHost.addTab(spec1,ClassTab[i], b );

    2.5 設置tab之間分割線顏色

    mTabHost.getTabWidget().setDividerDrawable(R.color.white)

    注意:不設置的話,Tab選項之間會有分隔線

     2.6 設置Tab按鈕的背景,當然也可以在XML文件中指定   

mTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.selector_tab_background);  

     2.7  關於XML

     2.7.1  兼容3.0以下使用 android.support.v4.app.FragmentTabHost命名空間;
     2.7.2  使用android:id=”@android:id/tabhost”系統命名
     2.7.3  tab選項卡的內容FrameLayout使用  android:id=”@android:id/tabcontent”系統命名
     2.7.4  TabWidget使用  android:id=”@android:id/tabs”系統命名


     常見佈局,最常見的就是這種

  1. <?xml version=“1.0” encoding=“utf-8”?>  
  2. <LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”  
  3.     android:layout_width=“fill_parent”  
  4.     android:layout_height=“fill_parent”  
  5.     android:orientation=“vertical” >  
  6.   
  7.     <FrameLayout  
  8.         android:id=“@+id/realtabcontent”  
  9.         android:layout_width=“fill_parent”  
  10.         android:layout_height=“0dip”  
  11.         android:layout_weight=“1” />  
  12.   
  13.     <android.support.v4.app.FragmentTabHost  
  14.         android:id=“@android:id/tabhost”  
  15.         android:layout_width=“fill_parent”  
  16.         android:layout_height=“wrap_content”   
  17.         android:background=“@drawable/maintab_toolbar_bg”>  
  18.   
  19.         <FrameLayout  
  20.             android:id=“@android:id/tabcontent”  
  21.             android:layout_width=“0dp”  
  22.             android:layout_height=“0dp”  
  23.             android:layout_weight=“0” />              
  24.     </android.support.v4.app.FragmentTabHost>  
  25.   
  26. </LinearLayout>  
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:id="@+id/realtabcontent"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1" />

    <android.support.v4.app.FragmentTabHost
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:background="@drawable/maintab_toolbar_bg">

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0" />            
    </android.support.v4.app.FragmentTabHost>

</LinearLayout>

     使用tabwidget,很多人說這種用法會報錯,其實用法大同小異,上面的佈局 setup時使用 R.id.realtabcontent,而這種使用android.R.id.tabcontent,下面的Demo中就使用這種

 

  1. <?xml version=“1.0” encoding=“utf-8”?>  
  2. <android.support.v4.app.FragmentTabHost xmlns:android=“http://schemas.android.com/apk/res/android”  
  3.     android:id=“@android:id/tabhost”  
  4.     android:layout_width=“match_parent”  
  5.     android:layout_height=“wrap_content” >  
  6.   
  7.     <LinearLayout  
  8.         android:layout_width=“fill_parent”  
  9.         android:layout_height=“fill_parent”  
  10.         android:orientation=“vertical” >  
  11.    
  12.         <!– 這裏是tab選項卡的內容 ,寬度要填滿,高度自動適應 –>  
  13.         <FrameLayout  
  14.             android:id=“@android:id/tabcontent”  
  15.             android:layout_width=“fill_parent”  
  16.             android:layout_height=“0dip”  
  17.             android:layout_weight=“1” >  
  18.         </FrameLayout>  
  19.   
  20.         <!– tabhost上面一條黑色分割 @drawable/line_shop –>  
  21.         <View  
  22.             android:id=“@+id/view_2”  
  23.             android:layout_width=“fill_parent”  
  24.             android:layout_height=“0.1dip”  
  25.             android:background=“#D1D1D1” >  
  26.         </View>  
  27.   
  28.         <!– 調換framelayout和tabwidget的前後順序可以分別實現tab的top和在底下的效果 –>  
  29.         <TabWidget  
  30.             android:id=“@android:id/tabs”  
  31.             android:layout_width=“fill_parent”  
  32.             android:layout_height=“wrap_content” >  
  33.         </TabWidget>  
  34.     </LinearLayout>  
  35.   
  36. </android.support.v4.app.FragmentTabHost>  
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <!-- 這裏是tab選項卡的內容 ,寬度要填滿,高度自動適應 -->
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1" >
        </FrameLayout>

        <!-- tabhost上面一條黑色分割 @drawable/line_shop -->
        <View
            android:id="@+id/view_2"
            android:layout_width="fill_parent"
            android:layout_height="0.1dip"
            android:background="#D1D1D1" >
        </View>

        <!-- 調換framelayout和tabwidget的前後順序可以分別實現tab的top和在底下的效果 -->
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
        </TabWidget>
    </LinearLayout>

</android.support.v4.app.FragmentTabHost>

     使用RadioGroup和 RadioButton定義Tab,這種需要給RadioGroup添加setOnCheckedChangeListener事件,不推薦

  

  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.   
  6.     <FrameLayout  
  7.         android:id=“@+id/content”  
  8.         android:layout_width=“match_parent”  
  9.         android:layout_height=“match_parent”  
  10.         android:layout_weight=“1” />  
  11.   
  12.     <RadioGroup  
  13.         android:id=“@+id/radioGroup1”  
  14.         android:layout_width=“match_parent”  
  15.         android:layout_height=“wrap_content”  
  16.         android:layout_alignParentBottom=“true”  
  17.         android:orientation=“horizontal” >  
  18.   
  19.         <RadioButton  
  20.             android:id=“@+id/radio0”  
  21.             android:layout_width=“wrap_content”  
  22.             android:layout_height=“wrap_content”  
  23.             android:layout_weight=“1”  
  24.             android:button=“@null”  
  25.             android:checked=“true”  
  26.             android:gravity=“center_horizontal”  
  27.             android:textColor=“@color/font_selector”  
  28.             android:textSize=“18dp”  
  29.             android:text=“首頁” />  
  30.   
  31.         <RadioButton  
  32.             android:id=“@+id/radio1”  
  33.             android:layout_width=“wrap_content”  
  34.             android:layout_height=“wrap_content”  
  35.             android:layout_weight=“1”  
  36.             android:button=“@null”  
  37.             android:gravity=“center_horizontal”  
  38.             android:textSize=“18dp”  
  39.             android:textColor=“@color/font_selector”  
  40.             android:text=“消息” />  
  41.   
  42.         <RadioButton  
  43.             android:id=“@+id/radio2”  
  44.             android:layout_width=“wrap_content”  
  45.             android:layout_height=“wrap_content”  
  46.             android:layout_weight=“1”  
  47.             android:button=“@null”  
  48.             android:gravity=“center_horizontal”  
  49.             android:textSize=“18dp”  
  50.             android:textColor=“@color/font_selector”  
  51.             android:text=“我的” />  
  52.     </RadioGroup>  
  53.   
  54. </LinearLayout>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:checked="true"
            android:gravity="center_horizontal"
            android:textColor="@color/font_selector"
            android:textSize="18dp"
            android:text="首頁" />

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center_horizontal"
            android:textSize="18dp"
            android:textColor="@color/font_selector"
            android:text="消息" />

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center_horizontal"
            android:textSize="18dp"
            android:textColor="@color/font_selector"
            android:text="我的" />
    </RadioGroup>

</LinearLayout>

3.  FragmentTabHost  DEMO效果圖和代碼

 


  3.1 定義主Tabmaintabs.xml

  

  1. <?xml version=“1.0” encoding=“utf-8”?>  
  2. <android.support.v4.app.FragmentTabHost xmlns:android=“http://schemas.android.com/apk/res/android”  
  3.     android:id=“@android:id/tabhost”  
  4.     android:layout_width=“match_parent”  
  5.     android:layout_height=“wrap_content” >  
  6.   
  7.     <LinearLayout  
  8.         android:layout_width=“fill_parent”  
  9.         android:layout_height=“fill_parent”  
  10.         android:orientation=“vertical” >  
  11.    
  12.         <!– 這裏是tab選項卡的內容 ,寬度要填滿,高度自動適應 –>  
  13.         <FrameLayout  
  14.             android:id=“@android:id/tabcontent”  
  15.             android:layout_width=“fill_parent”  
  16.             android:layout_height=“0dip”  
  17.             android:layout_weight=“1” >  
  18.         </FrameLayout>  
  19.   
  20.         <!– tabhost上面一條黑色分割 @drawable/line_shop –>  
  21.         <View  
  22.             android:id=“@+id/view_2”  
  23.             android:layout_width=“fill_parent”  
  24.             android:layout_height=“0.1dip”  
  25.             android:background=“#D1D1D1” >  
  26.         </View>  
  27.   
  28.         <!– 調換framelayout和tabwidget的前後順序可以分別實現tab的top和在底下的效果 –>  
  29.         <TabWidget  
  30.             android:id=“@android:id/tabs”  
  31.             android:layout_width=“fill_parent”  
  32.             android:layout_height=“wrap_content” >  
  33.         </TabWidget>  
  34.     </LinearLayout>  
  35.   
  36. </android.support.v4.app.FragmentTabHost>  
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <!-- 這裏是tab選項卡的內容 ,寬度要填滿,高度自動適應 -->
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1" >
        </FrameLayout>

        <!-- tabhost上面一條黑色分割 @drawable/line_shop -->
        <View
            android:id="@+id/view_2"
            android:layout_width="fill_parent"
            android:layout_height="0.1dip"
            android:background="#D1D1D1" >
        </View>

        <!-- 調換framelayout和tabwidget的前後順序可以分別實現tab的top和在底下的效果 -->
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
        </TabWidget>
    </LinearLayout>

</android.support.v4.app.FragmentTabHost>

  3.2 Tab按鈕 tab_main_home.xml

 

  1. <?xml version=“1.0” encoding=“utf-8”?>  
  2. <LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”  
  3.     android:id=“@+id/layout_back”  
  4.     android:layout_width=“match_parent”  
  5.     android:layout_height=“wrap_content”  
  6.     android:layout_gravity=“center”  
  7.     android:gravity=“center” >  
  8.   
  9.      
  10.     <TextView  
  11.         android:id=“@+id/textView1”  
  12.         android:layout_width=“wrap_content”  
  13.         android:layout_height=“wrap_content”  
  14.         android:paddingTop=“4dip”  
  15.         android:drawableTop=“@drawable/selector_home”  
  16.         android:focusable=“true”  
  17.         android:gravity=“center_horizontal”  
  18.         android:text=“首頁”  
  19.         android:textColor=“@drawable/tab_txt_sel”  
  20.         android:textSize=“14sp” />  
  21.   
  22. </LinearLayout>  
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_back"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center" >


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="4dip"
        android:drawableTop="@drawable/selector_home"
        android:focusable="true"
        android:gravity="center_horizontal"
        android:text="首頁"
        android:textColor="@drawable/tab_txt_sel"
        android:textSize="14sp" />

</LinearLayout>

  3.3 selector_home.xml

  1. <?xml version=“1.0” encoding=“utf-8”?>  
  2. <selector xmlns:android=“http://schemas.android.com/apk/res/android”>  
  3.   
  4.     <!– Non focused states –>  
  5.     <item android:drawable=“@drawable/icon_1_n” android:state_focused=“false” android:state_pressed=“false” android:state_selected=“false”/>  
  6.     <item android:drawable=“@drawable/icon_1_d” android:state_focused=“false” android:state_pressed=“false” android:state_selected=“true”/>  
  7.     <!– Focused states –>  
  8.     <item android:drawable=“@drawable/icon_1_d” android:state_focused=“true” android:state_pressed=“false” android:state_selected=“false”/>  
  9.     <item android:drawable=“@drawable/icon_1_d” android:state_focused=“true” android:state_pressed=“false” android:state_selected=“true”/>  
  10.     <!– Pressed –>  
  11.     <item android:drawable=“@drawable/icon_1_d” android:state_pressed=“true” android:state_selected=“true”/>  
  12.     <item android:drawable=“@drawable/icon_1_d” android:state_pressed=“true”/>  
  13.   
  14. </selector>  
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- Non focused states -->
    <item android:drawable="@drawable/icon_1_n" android:state_focused="false" android:state_pressed="false" android:state_selected="false"/>
    <item android:drawable="@drawable/icon_1_d" android:state_focused="false" android:state_pressed="false" android:state_selected="true"/>
    <!-- Focused states -->
    <item android:drawable="@drawable/icon_1_d" android:state_focused="true" android:state_pressed="false" android:state_selected="false"/>
    <item android:drawable="@drawable/icon_1_d" android:state_focused="true" android:state_pressed="false" android:state_selected="true"/>
    <!-- Pressed -->
    <item android:drawable="@drawable/icon_1_d" android:state_pressed="true" android:state_selected="true"/>
    <item android:drawable="@drawable/icon_1_d" android:state_pressed="true"/>

</selector>

  3.4 tab_txt_sel.xml

  

  1. <?xml version=“1.0” encoding=“utf-8”?>  
  2. <selector xmlns:android=“http://schemas.android.com/apk/res/android” >      
  3.     <!– Non focused states –>  
  4.     <item android:state_focused=“false” android:state_selected=“false” android:state_pressed=“false” android:color=“#1CBB9D” />  
  5.     <item android:state_focused=“false” android:state_selected=“true” android:state_pressed=“false” android:color=“#32A5E7” />  
  6.     <!– Focused states –>  
  7.     <item android:state_focused=“true” android:state_selected=“false” android:state_pressed=“false” android:color=“#32A5E7” />  
  8.     <item android:state_focused=“true” android:state_selected=“true” android:state_pressed=“false” android:color=“#32A5E7” />  
  9.     <!– Pressed –>  
  10.     <item android:state_selected=“true” android:state_pressed=“true” android:color=“#32A5E7” />  
  11.     <item android:state_pressed=“true” android:color=“#32A5E7” />  
  12. </selector>  
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >    
    <!-- Non focused states -->
    <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:color="#1CBB9D" />
    <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:color="#32A5E7" />
    <!-- Focused states -->
    <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:color="#32A5E7" />
    <item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:color="#32A5E7" />
    <!-- Pressed -->
    <item android:state_selected="true" android:state_pressed="true" android:color="#32A5E7" />
    <item android:state_pressed="true" android:color="#32A5E7" />
</selector>

  3.5  fragment_home.xml

  1. <RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”  
  2.     xmlns:tools=“http://schemas.android.com/tools”  
  3.     android:layout_width=“match_parent”  
  4.     android:layout_height=“match_parent”  
  5.    >  
  6.   
  7.     <TextView  
  8.         android:layout_centerInParent=“true”  
  9.         android:layout_width=“wrap_content”  
  10.         android:layout_height=“wrap_content”  
  11.         android:textSize=“25sp”  
  12.         android:text=“Fragment Home” />  
  13.   
  14. </RelativeLayout>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   >

    <TextView
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:text="Fragment Home" />

</RelativeLayout>


  3.6  MainTab,太簡單了,不作說明……

  

  1. public class MainTab extends FragmentActivity {  
  2.     private String TAG = MainTab.class.getName();  
  3.   
  4.     public FragmentTabHost mTabHost;  
  5.     // 標籤  
  6.     private String[] TabTag = { “tab1”“tab2”“tab3” };  
  7.     // 自定義tab佈局顯示文本和頂部的圖片  
  8.     private Integer[] ImgTab = { R.layout.tab_main_home,  
  9.             R.layout.tab_main_message, R.layout.tab_main_my };  
  10.   
  11.     // tab 選中的activity  
  12.     private Class[] ClassTab = { FragmentHome.class, FragmentMessage.class,  
  13.             FragmentMy.class };  
  14.   
  15.     // tab選中背景 drawable 樣式圖片 背景都是同一個,背景顏色都是 白色。。。  
  16.     private Integer[] StyleTab = { R.color.white, R.color.white, R.color.white,  
  17.             R.color.white };  
  18.   
  19.     @Override  
  20.     protected void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  23.         setContentView(R.layout.maintabs);  
  24.         setupView();  
  25.         initValue();  
  26.         setLinstener();  
  27.         fillData();  
  28.   
  29.     }  
  30.   
  31.     private void setupView() {  
  32.   
  33.         // 實例化framentTabHost  
  34.         mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);  
  35.         mTabHost.setup(this, getSupportFragmentManager(),  
  36.                 android.R.id.tabcontent);  
  37.   
  38.     }  
  39.   
  40.     private void initValue() {  
  41.   
  42.         // 初始化tab選項卡  
  43.         InitTabView();  
  44.     }  
  45.   
  46.     private void setLinstener() {  
  47.         // imv_back.setOnClickListener(this);  
  48.   
  49.     }  
  50.   
  51.     private void fillData() {  
  52.         // TODO Auto-generated method stub  
  53.   
  54.     }  
  55.   
  56.     // 初始化 tab 自定義的選項卡 ///////////////  
  57.     private void InitTabView() {  
  58.   
  59.         // 可以傳遞參數 b;傳遞公共的userid,version,sid  
  60.         Bundle b = new Bundle();  
  61.         // 循環加入自定義的tab  
  62.         for (int i = 0; i < TabTag.length; i++) {  
  63.             // 封裝的自定義的tab的樣式  
  64.             View indicator = getIndicatorView(i);  
  65.             mTabHost.addTab(  
  66.                     mTabHost.newTabSpec(TabTag[i]).setIndicator(indicator),  
  67.                     ClassTab[i], b);// 傳遞公共參數  
  68.   
  69.         }  
  70.         mTabHost.getTabWidget().setDividerDrawable(R.color.white);  
  71.     }  
  72.   
  73.     // 設置tab自定義樣式:注意 每一個tab xml子佈局的linearlayout 的id必須一樣  
  74.     private View getIndicatorView(int i) {  
  75.         // 找到tabhost的子tab的佈局視圖  
  76.         View v = getLayoutInflater().inflate(ImgTab[i], null);  
  77.         LinearLayout tv_lay = (LinearLayout) v.findViewById(R.id.layout_back);  
  78.         tv_lay.setBackgroundResource(StyleTab[i]);  
  79.   
  80.         return v;  
  81.     }  
  82. }  
public class MainTab extends FragmentActivity {
    private String TAG = MainTab.class.getName();

    public FragmentTabHost mTabHost;
    // 標籤
    private String[] TabTag = { "tab1", "tab2", "tab3" };
    // 自定義tab佈局顯示文本和頂部的圖片
    private Integer[] ImgTab = { R.layout.tab_main_home,
            R.layout.tab_main_message, R.layout.tab_main_my };

    // tab 選中的activity
    private Class[] ClassTab = { FragmentHome.class, FragmentMessage.class,
            FragmentMy.class };

    // tab選中背景 drawable 樣式圖片 背景都是同一個,背景顏色都是 白色。。。
    private Integer[] StyleTab = { R.color.white, R.color.white, R.color.white,
            R.color.white };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.maintabs);
        setupView();
        initValue();
        setLinstener();
        fillData();

    }

    private void setupView() {

        // 實例化framentTabHost
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(),
                android.R.id.tabcontent);

    }

    private void initValue() {

        // 初始化tab選項卡
        InitTabView();
    }

    private void setLinstener() {
        // imv_back.setOnClickListener(this);

    }

    private void fillData() {
        // TODO Auto-generated method stub

    }

    // 初始化 tab 自定義的選項卡 ///////////////
    private void InitTabView() {

        // 可以傳遞參數 b;傳遞公共的userid,version,sid
        Bundle b = new Bundle();
        // 循環加入自定義的tab
        for (int i = 0; i < TabTag.length; i++) {
            // 封裝的自定義的tab的樣式
            View indicator = getIndicatorView(i);
            mTabHost.addTab(
                    mTabHost.newTabSpec(TabTag[i]).setIndicator(indicator),
                    ClassTab[i], b);// 傳遞公共參數

        }
        mTabHost.getTabWidget().setDividerDrawable(R.color.white);
    }

    // 設置tab自定義樣式:注意 每一個tab xml子佈局的linearlayout 的id必須一樣
    private View getIndicatorView(int i) {
        // 找到tabhost的子tab的佈局視圖
        View v = getLayoutInflater().inflate(ImgTab[i], null);
        LinearLayout tv_lay = (LinearLayout) v.findViewById(R.id.layout_back);
        tv_lay.setBackgroundResource(StyleTab[i]);

        return v;
    }
}

Demo下載地址:http://download.csdn.net/detail/yalinfendou/8538361

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章