一、什麼是TabHost。
Android 裡面的TabHost就是選項卡,看(新浪微博介面):
至於選項卡有什麼好處或者用途,我想代碼哥們都知道吧,我就不多說了。
二、在Android裡面如何?TabHost
有兩種方式可以實現。
1、繼承TabActivity,然後用getTabHost()方法擷取TabHost,最後在布局檔案中定義各個Tab選項卡添加到TabHost中
2、不繼承TabActivity,然後在布局檔案中定義TabHost,最後講各個Tab選項卡添加到TabHost中
總結以上兩種方式為兩步:
①:擷取TabHost對象
②:把Tab添加到TabHost中。
我們先看第一種實現:
①:布局檔案:
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><!-- 第一個選項卡面板 --><LinearLayoutandroid:id="@+id/tab1"android:layout_width="fill_parent"android:layout_height="fill_parent"><!-- 面板中只有一個TextView--><TextView android:id="@+id/V1"android:layout_width="fill_parent"android:layout_height="fill_parent"android:text="Touch Android"/></LinearLayout><!-- 第二個選項卡面板 --><LinearLayoutandroid:id="@+id/tab2"android:layout_width="fill_parent"android:layout_height="fill_parent"><!-- 面板中只有一個TextView--><TextView android:id="@+id/V2"android:layout_width="fill_parent"android:layout_height="fill_parent"android:text="Touch Android"/></LinearLayout></LinearLayout>
②:Activity
package com.droidstouch.tabhost;import android.app.TabActivity;import android.os.Bundle;import android.view.LayoutInflater;import android.widget.TabHost;import android.widget.TabHost.TabSpec;/*** @author <a href="http://bbs.droidstouch.com">Touch Android</a>**/public class Demo2Activity extends TabActivity{protected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);// this.setContentView(R.layout.demo2); // 注意不要加上此行代碼//擷取到TabHost對象TabHost tabHost =this.getTabHost();//把我們的布局檔案添加到tabHost 的FrameLayout下面LayoutInflater.from(this).inflate(R.layout.demo2, tabHost.getTabContentView(), true);// 下面定義了兩個選項卡//擷取一個新的TabHost.TabSpec,並關聯到當前tab host//參數:所需的選項卡標籤TabSpec pSpec = tabHost.newTabSpec("parent");// 參數一:選項卡上的文字,參數二:選項卡的背景圖片pSpec.setIndicator("父類", this.getResources().getDrawable(R.drawable.f_root));//設定選項卡內容pSpec.setContent(R.id.tab1);TabSpec subSpec = tabHost.newTabSpec("sub");subSpec.setIndicator("子類", this.getResources().getDrawable(R.drawable.f_sub));subSpec.setContent(R.id.tab2);// 將選項卡添加到TabHost中tabHost.addTab(pSpec);tabHost.addTab(subSpec);}}
第二種方式
①:布局檔案
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><!-- 定義TabHost ,自訂的TabHost一定得包含TabWidget 和 FrameLayout,並且 TabWidget 的ID一定是@android:id/tabsFrameLayout 的Id一定是@android:id/tabcontent--><TabHost android:id="@+id/tabs"android:layout_width="fill_parent"android:layout_height="fill_parent"><LinearLayout android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><!-- 定義TabWidget,此控制項 必須和TabHost一起使用 --><TabWidget android:id="@android:id/tabs"android:layout_width="fill_parent"android:layout_height="wrap_content"/><!-- 定義FrameLayout--><FrameLayout android:id="@android:id/tabcontent"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextView android:id="@+id/txtV1"android:layout_width="fill_parent"android:layout_height="fill_parent"android:text="Touch Android"/><TextView android:id="@+id/txtV2"android:layout_width="fill_parent"android:layout_height="fill_parent"android:text="http://bbs.droidstouch.com"/></FrameLayout></LinearLayout></TabHost></LinearLayout>
②:Activity:
package com.droidstouch.tabhost;import android.app.Activity;import android.os.Bundle;import android.widget.TabHost;import android.widget.TabHost.TabSpec;public class Dome1Activity extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.demo1);//從布局檔案中 擷取到TabHostTabHost tabHost = (TabHost) this.findViewById(R.id.tabs);//安裝TabHosttabHost.setup();// 下面定義兩個選項卡//擷取一個新的TabHost.TabSpec,並關聯到當前tab host//參數:所需的選項卡標籤TabSpec pSpec = tabHost.newTabSpec("parent");pSpec.setIndicator("父類", this.getResources().getDrawable(R.drawable.f_root));pSpec.setContent(R.id.txtV1);TabSpec subSpec = tabHost.newTabSpec("sub");subSpec.setIndicator("子類", this.getResources().getDrawable(R.drawable.f_root));subSpec.setContent(R.id.txtV2);//添加選項卡到TabHost中tabHost.addTab(pSpec);tabHost.addTab(subSpec);}}