TabHost+Fragment的應(yīng)用

很久沒有應(yīng)用了,東西都忘記了,總結(jié)出來,曾經(jīng)的東西在撿起來一次,加強(qiáng)下記憶

上代碼:

  • 在XML的布局:
 <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/backgroudColor">
        <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="55dp"
                android:background="@color/wirteColor"/>
        <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/wirteColor">
                <LinearLayout
                    android:id="@+id/line_one"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/line_two"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/line_three"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                </LinearLayout>
        </FrameLayout>
</TabHost>
  • java類中的實(shí)現(xiàn):
TabHost mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
//添加“主頁”Tab到TabHost控件中
 mTabHost.addTab(mTabHost.newTabSpec("home")//Tag
                .setIndicator("HOME")//設(shè)置Tab標(biāo)簽和圖標(biāo)
                .setContent(R.id.line_one));        //設(shè)置Tab內(nèi)容
//添加“消息”Tab到TabHost控件中
 mTabHost.addTab(mTabHost.newTabSpec("news")
                .setIndicator("NEWS")
                .setContent(R.id.line_two));
//添加“個(gè)人”Tab到TabHost控件中
mTabHost.addTab(mTabHost.newTabSpec("mine")
                .setIndicator("MINE")
                .setContent(R.id.line_three));
//設(shè)置當(dāng)前默認(rèn)顯示第一個(gè)Tab
mTabHost.setCurrentTab(0);
 //TabHost改變監(jiān)聽
mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
    @Override
    public void onTabChanged(String tabId) {
        Log.d("===>>>onTabChanged",tabId.toString());
    }
});
  • 下來我們要?jiǎng)?chuàng)建屬于三個(gè)界面的Fragment(HomeFragment.NewsFragment,MineFragment)
  • 最后分別添加到TabHost的tabcontent中,tabcontent里面有幾個(gè)子空間,它就有幾個(gè)tab
//home
HomeFragment nomeFragment= new HomeFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.line_one, homeFragment).commit();
//news
NewsFragment newsFragment = new NewsFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.line_two, newsFragment ).commit();
//mine
MineFragment mineFragment= new MineFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.line_three, mineFragment).commit();
  • 當(dāng)然了Tab的標(biāo)簽和圖標(biāo)可以自定義一個(gè)View,通過drawable來設(shè)置每個(gè)Tab選中時(shí)的顏色等...(下面只試一個(gè):在layout下定義一個(gè)home_item.xml

<!--        === layout中 ===          -->
//只在布局中添加下面就行了,再去drawable創(chuàng)建選中時(shí)的字體顏色--home_item_selected
<TextView
        android:textSize="@dimen/sp18"
        android:textColor="@drawable/home_item_selected"
        android:text="@string/string_home"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

<!--        === drawable中 ===          -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:color="@color/appthemeColor" />
    <item android:color="@color/textColor" />
</selector>

好了,就這么簡(jiǎn)單的完了,當(dāng)然了,TabHost+Fragment的寫法有好幾種,我這里只用了最簡(jiǎn)單的這一種,當(dāng)然,現(xiàn)在由于項(xiàng)目的需求,有些也用不上此方法,因?yàn)檫@個(gè)加載數(shù)據(jù)是一次性全部執(zhí)行,三個(gè)Fragment的生命周期一起執(zhí)行的

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容