詳解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ì)容易一些,下面來看看分別是怎樣來實(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)簽頁布局 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)容分開:不用繼承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)頁面中多個(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仿新浪微博啟動(dòng)界面或登陸界面(1)
這篇文章主要為大家詳細(xì)介紹了Android仿新浪微博啟動(dòng)界面或登陸界面的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11一款非常簡(jiǎn)單酷炫的LoadingView動(dòng)畫效果
這篇文章主要為大家詳細(xì)介紹了一款非常簡(jiǎn)單酷炫的LoadingView動(dòng)畫效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08Flutter?WebView?預(yù)加載實(shí)現(xiàn)方法(Http?Server)
這篇文章主要介紹了Flutter?WebView?預(yù)加載實(shí)現(xiàn)方法,包括資源的配置,資源的下載和存儲(chǔ),版本的管理,如何根據(jù)實(shí)際url獲取對(duì)應(yīng)HttpServer?bind的url等,需要的朋友可以參考下2022-05-05Android WindowManger的層級(jí)分析詳解
這篇文章主要介紹了Android WindowManger的層級(jí)分析詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09android中打開相機(jī)、打開相冊(cè)進(jìn)行圖片的獲取示例
本篇文章主要介紹了android中打開相機(jī)、打開相冊(cè)進(jìn)行圖片的獲取示例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-01-01Android性能優(yōu)化之弱網(wǎng)優(yōu)化詳解
這篇文章主要為大家介紹了Android性能優(yōu)化之弱網(wǎng)優(yōu)化示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10簡(jiǎn)析Android五大布局(LinearLayout、FrameLayout、RelativeLayout等)
這篇文章主要為大家簡(jiǎn)單分析了Android五大布局,內(nèi)容有LinearLayout、FrameLayout、RelativeLayout、AbsoluteLayout和TableLayout的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-06-06Android實(shí)現(xiàn)簡(jiǎn)單動(dòng)態(tài)搜索功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)單動(dòng)態(tài)搜索功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05Android 讀取文件內(nèi)容實(shí)現(xiàn)方法總結(jié)
這篇文章主要介紹了Android 讀取文件內(nèi)容實(shí)現(xiàn)方法的相關(guān)資料,這里提供了幾種方法,大家可以選擇使用,需要的朋友可以參考下2016-10-10Moshi?完美解決Gson在kotlin中默認(rèn)值空的問題詳解
這篇文章主要為大家介紹了Moshi?完美解決Gson在kotlin中默認(rèn)值空的問題詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03