詳解Android TabHost的多種實(shí)現(xiàn)方法 附源碼下載
最近仔細(xì)研究了下TabHost,主要是為了實(shí)現(xiàn)微信底部導(dǎo)航欄的功能,最后也給出一個(gè)文章鏈接,大家不要著急
正文:
TabHost的實(shí)現(xiàn)分為兩種,一個(gè)是不繼承TabActivity,一個(gè)是繼承自TabActivity;當(dāng)然了選用繼承自TabActivity的話就相對(duì)容易一些,下面來(lái)看看分別是怎樣來(lái)實(shí)現(xiàn)的吧。
方法一、定義tabhost:不用繼承TabActivity
1、布局文件:activity_main.xml
<LinearLayout 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" android:orientation="vertical" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> <TabHost android:id="@+id/tabhost" android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" > </TabWidget> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent" > <!-- 第一個(gè)tab的布局 --> <LinearLayout android:id="@+id/tab1" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="林炳東" /> </LinearLayout> <!-- 第二個(gè)tab的布局 --> <LinearLayout android:id="@+id/tab2" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="張小媛" /> </LinearLayout> <!-- 第三個(gè)tab的布局 --> <LinearLayout android:id="@+id/tab3" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="馬貝貝" /> </LinearLayout> </FrameLayout> </LinearLayout> </TabHost> </LinearLayout>
2、JAVA代碼
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost th=(TabHost)findViewById(R.id.tabhost);
th.setup(); //初始化TabHost容器
//在TabHost創(chuàng)建標(biāo)簽,然后設(shè)置:標(biāo)題/圖標(biāo)/標(biāo)簽頁(yè)布局
th.addTab(th.newTabSpec("tab1").setIndicator("標(biāo)簽1",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.tab1));
th.addTab(th.newTabSpec("tab2").setIndicator("標(biāo)簽2",null).setContent(R.id.tab2));
th.addTab(th.newTabSpec("tab3").setIndicator("標(biāo)簽3",null).setContent(R.id.tab3));
//上面的null可以為getResources().getDrawable(R.drawable.圖片名)設(shè)置圖標(biāo)
}
}
效果圖:

此例源碼地址:Demo1
方法二、Tab的內(nèi)容分開(kāi):不用繼承TabActivity
1、第一個(gè)tab的XML布局文件,tab1.xml:
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/LinearLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:text="我是標(biāo)簽1的內(nèi)容喔" android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content"> </TextView> </LinearLayout>
2、第二個(gè)tab的XML布局文件,tab2.xml:
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/LinearLayout02" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:text="標(biāo)簽2" android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
3、主布局文件,activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <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" > <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" > </TabWidget> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent" > </FrameLayout> </LinearLayout> </TabHost> </LinearLayout>
4、JAVA代碼:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost m = (TabHost)findViewById(R.id.tabhost);
m.setup();
LayoutInflater i=LayoutInflater.from(this);
i.inflate(R.layout.tab1, m.getTabContentView());
i.inflate(R.layout.tab2, m.getTabContentView());//動(dòng)態(tài)載入XML,而不需要Activity
m.addTab(m.newTabSpec("tab1").setIndicator("標(biāo)簽1").setContent(R.id.LinearLayout01));
m.addTab(m.newTabSpec("tab2").setIndicator("標(biāo)簽2").setContent(R.id.LinearLayout02));
}
}
效果圖:

此例源碼地址:Demo2
方法三、繼承自TabActivity
1、主布局文件,activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <!-- 第一個(gè)布局 --> <LinearLayout android:id="@+id/view1" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="張小媛" /> </LinearLayout> <!-- 第二個(gè)布局 --> <LinearLayout android:id="@+id/view2" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="馬貝貝" /> </LinearLayout> <!-- 第三個(gè)布局 --> <TextView android:id="@+id/view3" android:background="#00ff00" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="Tab3"/> </FrameLayout>
2、JAVA代碼:
先將派生自Activity改為TabActivity,然后代碼如下:
public class MainActivity extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("TabDemoActivity");
TabHost tabHost = getTabHost();
LayoutInflater.from(this).inflate(R.layout.activity_main,
tabHost.getTabContentView(), true);
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("tab1", getResources().getDrawable(R.drawable.ic_launcher))
.setContent(R.id.view1));
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("tab2")
.setContent(R.id.view2));
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("tab3")
.setContent(R.id.view3));
//標(biāo)簽切換事件處理,setOnTabChangedListener
tabHost.setOnTabChangedListener(new OnTabChangeListener(){
@Override
public void onTabChanged(String tabId) {
if (tabId.equals("tab1")) { //第一個(gè)標(biāo)簽
}
if (tabId.equals("tab2")) { //第二個(gè)標(biāo)簽
}
if (tabId.equals("tab3")) { //第三個(gè)標(biāo)簽
}
}
});
}
}
效果圖:

此例源碼地址:Demo3
四、實(shí)現(xiàn)微信底部導(dǎo)航欄
效果:

參看:Android仿微信底部菜單欄功能顯示未讀消息數(shù)量
原文地址:http://blog.csdn.net/harvic880925/article/details/17120325
以上就是本文的全部?jī)?nèi)容,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Android組件TabHost實(shí)現(xiàn)頁(yè)面中多個(gè)選項(xiàng)卡切換效果
- android TabHost(選項(xiàng)卡)的使用方法
- android 選項(xiàng)卡(TabHost)如何放置在屏幕的底部
- Android組件必學(xué)之TabHost使用方法詳解
- Android控件之TabHost用法實(shí)例分析
- Android 使用FragmentTabhost代替Tabhost
- 詳解Android應(yīng)用中使用TabHost組件進(jìn)行布局的基本方法
- Android編程實(shí)現(xiàn)設(shè)置TabHost當(dāng)中字體的方法
- Android Tabhost使用方法詳解
- Android TabHost組件使用方法詳解
- Android TabHost選項(xiàng)卡標(biāo)簽圖標(biāo)始終不出現(xiàn)的解決方法
相關(guān)文章
android基于ListView和CheckBox實(shí)現(xiàn)多選和全選記錄的功能
本篇文章主要介紹了android基于ListView和CheckBox實(shí)現(xiàn)多選和全選記錄的功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-11-11
Android中制作進(jìn)度框和環(huán)形進(jìn)度條的簡(jiǎn)單實(shí)例分享
這篇文章主要介紹了Android中制作進(jìn)度框和環(huán)形進(jìn)度條的簡(jiǎn)單實(shí)例分享,環(huán)形進(jìn)度條帶有基本的百分比顯示,需要的朋友可以參考下2016-03-03
在android開(kāi)發(fā)中進(jìn)行數(shù)據(jù)存儲(chǔ)與訪問(wèn)的多種方式介紹
很多時(shí)候我們的軟件需要對(duì)處理后的數(shù)據(jù)進(jìn)行存儲(chǔ)或再次訪問(wèn),Android為數(shù)據(jù)存儲(chǔ)提供了多種方式,首先給大家介紹使用文件如何對(duì)數(shù)據(jù)進(jìn)行存儲(chǔ),感興趣的朋友可以了解下哈2013-06-06
Android實(shí)現(xiàn)無(wú)限循環(huán)滾動(dòng)彈幕的代碼示例
這篇文章主要介紹了android實(shí)現(xiàn)無(wú)限循環(huán)滾動(dòng)的彈幕2024-08-08
,文中通過(guò)代碼示例講解的非常詳細(xì),對(duì)大家實(shí)現(xiàn)循環(huán)彈幕有一定的幫助,需要的朋友可以參考下
Flutter質(zhì)感設(shè)計(jì)之彈出菜單
這篇文章主要為大家詳細(xì)介紹了Flutter質(zhì)感設(shè)計(jì)之彈出菜單,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
完美解決android M上鎖屏情況下,禁止pc通過(guò)MTP訪問(wèn)手機(jī)存儲(chǔ)單元
下面小編就為大家?guī)?lái)一篇完美解決android M上鎖屏情況下,禁止pc通過(guò)MTP訪問(wèn)手機(jī)存儲(chǔ)單元。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04

