FragmentTabHost繼承TabHost,增加了對Fragment的支持。
Fragment是從Android3.0才引入,Fragment有自己單獨的生命週期,在Activity運行的時候可以很方便的使用。
這裏只是介紹FragmentTabHost的用法。
1. 先看看谷歌官方文檔上的兩個例子:
- 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);
- }
- }
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);
}
}
- 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;
- }
- }
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;
}
}
第二個例子是在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(Context context, AttributeSet attrs)
2.2 對FragmentTabHost做初始化的操作
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, Drawable icon);第二種可以使用文字+icon標識tab
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”系統命名
常見佈局,最常見的就是這種
- <?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>
<?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中就使用這種
- <?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>
<?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事件,不推薦
- <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>
<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
- <?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>
<?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
- <?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>
<?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
- <?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>
<?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
- <?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>
<?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
- <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>
<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,太簡單了,不作說明……
- 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;
- }
- }
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