TabHost ~ 仿微信底部菜單

簡(jiǎn)介

Container for a tabbed window view. This object holds two children: a set of tab labels that theuser clicks to select a specific tab, and a FrameLayout object that displays the contents of thatpage. The individual elements are typically controlled using this container object, rather thansetting values on the child elements themselves.
TabHost 犀斋,標(biāo)簽視圖的容器。容器包含兩個(gè)孩子節(jié)點(diǎn)搔弄,一個(gè)用來(lái)存放一系列的標(biāo)簽,點(diǎn)擊來(lái)選擇對(duì)應(yīng)的窗口椿疗;一個(gè)是FrameLayout用來(lái)存放頁(yè)面具體內(nèi)容萍虽。這些獨(dú)立的元素通常用TabHost來(lái)控制轴脐,而不是在視圖內(nèi)部通過(guò)設(shè)置值來(lái)實(shí)現(xiàn)


類結(jié)構(gòu)

類結(jié)構(gòu)
方法 意義
addTab 添加一個(gè)tab
clearAllTabs 移除所有的tab
dispatchKeyEvent 下發(fā)keyevent
dispatchWindowFocusChanged 下發(fā)windowsfocusChanged事件
newTabSpec 創(chuàng)建一個(gè)新的TabSpec,關(guān)聯(lián)到具體內(nèi)容
onTouchModeChanged NA
setup() 不和TabActivity關(guān)聯(lián)砾嫉,通過(guò)findViewById獲取的TabHost需要調(diào)用setup(),如果是在TabActivity中通過(guò)getTabHost()的方式獲取的不需要調(diào)用這個(gè)方法
setup(LocalActivityManager activityGroup) setContent中傳入intent的時(shí)候才關(guān)注下這個(gè)方法
getCurrentTab()/setCurrentTab() 獲取/設(shè)置當(dāng)前需要顯示的tab幼苛,通過(guò)index
setCurrentTabByTag/getCurrentTabTag 通過(guò)tag設(shè)置當(dāng)前需要顯示的Tab,tag就是創(chuàng)建TabSpec的時(shí)候傳入的字符串
getCurrentTabView 設(shè)置/獲取當(dāng)前在TabWidget中顯示的View,也就是作為標(biāo)簽的View而非內(nèi)容
getCurrentView 獲取當(dāng)前顯示的內(nèi)容
setOnTabChangedListener 設(shè)置標(biāo)簽頁(yè)切換事件監(jiān)聽(tīng)
getTabContentView 獲取內(nèi)容頁(yè)面的容器FrameLayout
getTabWidget 獲取TabWidget

基本使用

1. 布局文件(content_fragment.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"         
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical">

     <android.support.v7.widget.Toolbar 
        android:id="@+id/toolbar" 
        android:layout_width="match_parent" 
        android:layout_height="?attr/actionBarSize"     
        android:background="@color/colorTopBackGround">
    </android.support.v7.widget.Toolbar> 

    <TabHost 
        android:id="@+id/tabhost" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"> 
       <LinearLayout 
            android:layout_width="match_parent" 
            android:layout_height="match_parent" 
            android:orientation="vertical"> 

            <!-- 設(shè)置的id必須是 "@android:id/tabcontent"-->
            <FrameLayout android:id="@android:id/tabcontent"  
                android:layout_width="match_parent"
                android:layout_height="0dp" 
                android:layout_weight="9"> 
                <TextView android:id="@+id/tv_one" 
                    android:layout_width="match_parent" 
                    android:layout_height="match_parent" 
                    android:text="This is one" 
                    android:textColor="@color/colorAccent"
                    android:textSize="20sp" /> 
                <TextView android:id="@+id/tv_two" 
                    android:layout_width="match_parent" 
                    android:layout_height="match_parent" 
                    android:text="This is two" 
                    android:textColor="@color/colorPrimary" 
                    android:textSize="25sp" /> 
                <TextView android:id="@+id/tv_three" 
                    android:layout_width="match_parent" 
                    android:layout_height="match_parent" 
                    android:text="This is three"
                    android:textColor="@color/colorPrimaryDark" 
                    android:textSize="30sp" />
                <TextView android:id="@+id/tv_four" 
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="This is Four" 
                    android:textColor="@color/colorBlack"
                    android:textSize="35sp" /> 
        </FrameLayout>
        <!-- 設(shè)置的id必須是android:id="@android:id/tabs" -->
        <TabWidget android:id="@android:id/tabs" 
            android:layout_width="match_parent"
            android:layout_height="0dp" 
            android:layout_weight="1" 
            android:background="@color/colorWhite" 
            android:padding="5dp" 
            android:showDividers="none">
        </TabWidget> 
       </LinearLayout> 
    </TabHost>
</LinearLayout>

2. 自定義底部標(biāo)簽布局(myindicator.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     
    android:layout_width="match_parent" 
    android:layout_height="match_parent"
    android:orientation="vertical">
    <ImageView android:id="@+id/iv_indicator"
        android:layout_width="match_parent"
        android:layout_height="0dp" 
        android:layout_weight="5">
    </ImageView>
    <TextView android:id="@+id/tv_indicator" 
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2.5" 
        android:gravity="center" android:textSize="5pt" />
</LinearLayout>

底部一個(gè)圖標(biāo)下面一段文字

3. 代碼使用(MainActivity.java),不使用TabActivity

public class MainActivity extends AppCompatActivity {
    float initx = 0.0f, currentx = 0.0f;
    TabHost tabHost = null;
    Toolbar toolbar = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_fragment);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);//設(shè)置ActionBar
        toolbar.setTitleTextColor(getResources().getColor(R.color.colorWhite));//設(shè)置背景色
        tabHost = (TabHost) findViewById(R.id.tabhost);
        tabHost.setup();//初始化TabHost
        /*添加tab*/
        for (int i = 0; i < 4; i++) {
            View view = LayoutInflater.from(this).inflate(R.layout.myindicator, null, false);
            TextView textView = (TextView) view.findViewById(R.id.tv_indicator);
            ImageView imageView = (ImageView) view.findViewById(R.id.iv_indicator);

            switch (i) {
                case 0:
                    textView.setText("微信");
                    imageView.setImageResource(R.drawable.weixin);
                    tabHost.addTab(tabHost.newTabSpec("1").setIndicator(view).setContent(R.id.tv_one));
                    break;
                case 1:
                    textView.setText("通訊錄");
                    imageView.setImageResource(R.drawable.contact);
                    tabHost.addTab(tabHost.newTabSpec("2").setIndicator(view).setContent(R.id.tv_two));
                    break;
                case 2:
                    textView.setText("發(fā)現(xiàn)");
                    imageView.setImageResource(R.drawable.find);
                    tabHost.addTab(tabHost.newTabSpec("3").setIndicator(view).setContent(R.id.tv_three));
                    break;
                case 3:
                    textView.setText("我");
                    imageView.setImageResource(R.drawable.profile);
                    tabHost.addTab(tabHost.newTabSpec("4").setIndicator(view).setContent(R.id.tv_four));
                    break;
            }

        }
        /*設(shè)置標(biāo)簽切換監(jiān)聽(tīng)器*/
        tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                for (int i = 0; i < 4; i++) {//顏色全部重置
                    ((TextView) tabHost.getTabWidget().getChildTabViewAt(i).findViewById(R.id.tv_indicator)).setTextColor(getResources().getColor(R.color.colorBlack));
                }
                if (tabHost.getCurrentTabTag() == tabId) {
                    ((TextView) tabHost.getCurrentTabView().findViewById(R.id.tv_indicator)).setTextColor(getResources().getColor(R.color.colorSelected));
                }//選中的那個(gè)Tab文字顏色修改
            }
        });
        ((TextView) tabHost.getCurrentTabView().findViewById(R.id.tv_indicator)).setTextColor(getResources().getColor(R.color.colorSelected));//第一次進(jìn)入的時(shí)候講選中的Tab修改文字顏色

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                initx = event.getX();
                break;
            case MotionEvent.ACTION_MOVE:
                currentx = event.getX();
                break;
            case MotionEvent.ACTION_UP:
                /*左右滑動(dòng)事件處理*/
                if ((currentx - initx) > 25) {
                    if (tabHost.getCurrentTab() != 0) {                      tabHost.setCurrentTab(tabHost.getCurrentTab() - 1);
                    }
                } else if ((currentx - initx) < -25) {
                    if (tabHost.getCurrentTab() != tabHost.getTabContentView().getChildCount()) {
                        tabHost.setCurrentTab(tabHost.getCurrentTab() + 1);
                    }
                }
                break;

        }

        return true;
    }

    /*菜單創(chuàng)建*/
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar, menu);
        return true;
    }
}

4. 代碼使用(MainActivity.java)焕刮,繼承TabActivity

TabActivity已經(jīng)被廢棄很久了舶沿,但是還是可以使用舌剂,在布局中將TabHost的id改成android:id="@android:id/tabhost",然后在繼承了TabActivity的MainActivity.java中使用tabHost = getTabHost();然后基本使用方法就和上面一樣了


實(shí)際效果

實(shí)際效果

寫在最后

TabHost在TabActivity中的使用現(xiàn)在開(kāi)發(fā)過(guò)程中使用的不多暑椰,推薦使用ViewPager和Fragment的方式

我的CSDN博客地址:
http://blog.csdn.net/poorkick/article/details/53394047

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末霍转,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子一汽,更是在濱河造成了極大的恐慌避消,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,386評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件召夹,死亡現(xiàn)場(chǎng)離奇詭異岩喷,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)监憎,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,142評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門纱意,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人鲸阔,你說(shuō)我怎么就攤上這事偷霉。” “怎么了褐筛?”我有些...
    開(kāi)封第一講書人閱讀 164,704評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵类少,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我渔扎,道長(zhǎng)硫狞,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書人閱讀 58,702評(píng)論 1 294
  • 正文 為了忘掉前任晃痴,我火速辦了婚禮残吩,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘倘核。我一直安慰自己泣侮,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,716評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布笤虫。 她就那樣靜靜地躺著旁瘫,像睡著了一般祖凫。 火紅的嫁衣襯著肌膚如雪琼蚯。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書人閱讀 51,573評(píng)論 1 305
  • 那天惠况,我揣著相機(jī)與錄音遭庶,去河邊找鬼。 笑死稠屠,一個(gè)胖子當(dāng)著我的面吹牛峦睡,可吹牛的內(nèi)容都是我干的翎苫。 我是一名探鬼主播,決...
    沈念sama閱讀 40,314評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼榨了,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼煎谍!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起龙屉,我...
    開(kāi)封第一講書人閱讀 39,230評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤呐粘,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后转捕,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體作岖,經(jīng)...
    沈念sama閱讀 45,680評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,873評(píng)論 3 336
  • 正文 我和宋清朗相戀三年五芝,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了痘儡。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,991評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡枢步,死狀恐怖沉删,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情醉途,我是刑警寧澤丑念,帶...
    沈念sama閱讀 35,706評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站结蟋,受9級(jí)特大地震影響脯倚,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜嵌屎,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,329評(píng)論 3 330
  • 文/蒙蒙 一推正、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧宝惰,春花似錦植榕、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 31,910評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至淤堵,卻和暖如春寝衫,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背拐邪。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 33,038評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工慰毅, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人扎阶。 一個(gè)月前我還...
    沈念sama閱讀 48,158評(píng)論 3 370
  • 正文 我出身青樓汹胃,卻偏偏與公主長(zhǎng)得像婶芭,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子着饥,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,941評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容