1. TabHost常用組件
TabWidget : 該組件就是TabHost標籤頁中上部 或者 下部的按鈕, 可以點擊按鈕切換選項卡;
TabSpec : 代表了選項卡界面, 添加一個TabSpec即可添加到TabHost中;
-- 創建選項卡 : newTabSpec(String tag), 創建一個選項卡;
-- 添加選項卡 : addTab(tabSpec);
2. TabHost使用步驟
a. 定義佈局 : 在XML文件中使用TabHost組件, 並在其中定義一個FrameLayout選項卡內容;
b. 繼承TabActivity : 顯示選項卡組件的Activity繼承TabActivity;
c. 獲取組件 : 通過調用getTabHost()方法, 獲取TabHost對象;
d. 創建添加選項卡 : 通過TabHost創建添加選項卡;
3. 將按鈕放到下面
佈局文件中TabWidget代表的就是選項卡按鈕, Fragement組件代表內容;
設置失敗情況 : 如果Fragement組件沒有設置 android:layout_weight屬性, 那麼將TabWidget放到下面, 可能不會顯示按鈕;
設置權重 : 設置了Fragment組件的權重之後, 就可以成功顯示該選項卡按鈕;
二. TabHost佈局文件
1. 根標籤及id
設置Android自帶id : XML佈局文件中, 可以使用 標籤設置, 其中的id 需要引用 android的自帶id : android:id=@android:id/tabhost ;
getHost()獲取前提 : 設置了該id之後, 在Activity界面可以使用 getHost(), 獲取這個TabHost 視圖對象;
2. TabWidget組件
選項卡切換 : 該組件是選項卡切換按鈕, 通過點擊該組件可以切換選項卡;
設置android自帶id : 這個組件的id要設置成android的自帶id : android:id=@android:id/tabs ;
TabHost必備組件 : 該組件與FrameLayout組件是TabHost組件中必備的兩個組件;
切換按鈕下方顯示 : 如果想要將按鈕放到下面, 可以將該組件定義在下面, 但是注意,FrameLayout要設置android:layout_widget = 1;
設置TabWidget大小 : 如果想要設置該按鈕組件的大小, 可以設置該組件與FrameLayout組件的權重;
示例 :
1
|
<tabwidget android:id= "@android:id/tabs"
android:layout_height= "wrap_co |
3. FrameLayout組件
組件作用 : 該組件中定義的子組件是TabHost中每個頁面顯示的選項卡, 可以將TabHost選項卡顯示的視圖定義在其中;
設置android自帶id : 這個組件的id要設置成android的自帶的id : android:id=@android:id/tabcontent ;
示例 :
1
|
<framelayout android:id= "@android:id/tabcontent"
android:layout_height=
|
二. Activity方法
1. 獲取TabHost
獲取方法 : getHost();
前提 : 調用getHost()方法獲取TabHost組件的方法的前提是在佈局文件中, 設置了android自帶的id android:id=@android:id/tabhost 纔可以;
2. 創建選項卡
創建選項卡 : 調用TabHost組件的newTabHost(tag), 其中的tag是字符串, 即在選項卡的唯一標識;
設置選項卡 :
-- 設置按鈕名稱 : setIndicator(叫獸);
-- 設置選項卡內容 : setContent(), 可以設置視圖組件, 可以設置Activity, 也可以設置Fragement;
添加選項卡 : tabHost.add(tag), 傳入的參數是創建選項卡的時候定義的唯一標識;
三 代碼
XML佈局文件 :
<!--?xml version=1.0 encoding=utf-8?-->
<tabhost android:id="@android:id/tabhost" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<linearlayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical">
<tabwidget android:id="@android:id/tabs" android:layout_height="wrap_content" android:layout_width="fill_parent" android:orientation="horizontal/">
<framelayout android:id="@android:id/tabcontent" android:layout_height="fill_parent" android:layout_weight="1" android:layout_width="fill_parent">
<linearlayout android:id="@+id/alwayswet" android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical">
<imageview android:layout_height="fill_parent" android:layout_width="fill_parent" android:scaletype="fitXY" android:src="@drawable/alwayswet/">
</imageview></linearlayout>
<linearlayout android:id="@+id/isanimal" android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical">
<imageview android:layout_height="fill_parent" android:layout_width="fill_parent" android:scaletype="fitXY" android:src="@drawable/isanimal/">
</imageview></linearlayout>
<linearlayout android:id="@+id/nezha" android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical">
<imageview android:layout_height="fill_parent" android:layout_width="fill_parent" android:scaletype="fitXY" android:src="@drawable/nazha/">
</imageview></linearlayout>
</framelayout>
</tabwidget></linearlayout>
</tabhost>
Activity主界面代碼 :
package shuliang.han.tabhost_test;
import android.app.TabActivity;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class MainActivity extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabhost);
TabHost tabHost = getTabHost();
TabSpec page1 = tabHost.newTabSpec(tab1)
.setIndicator(叫獸)
.setContent(R.id.isanimal);
tabHost.addTab(page1);
TabSpec page2 = tabHost.newTabSpec(tab2)
.setIndicator(老溼)
.setContent(R.id.alwayswet);
tabHost.addTab(page2);
TabSpec page3 = tabHost.newTabSpec(tab3)
.setIndicator(哪吒)
.setContent(R.id.nezha);
tabHost.addTab(page3);
}
}