标簽元件Tabhost類似于Windows應用中的選項框。
TabHost的實作有兩種方式,第一種繼承TabActivity,從TabActivity中用getTabHost()方法擷取TabHost。各個Tab中的内容在布局檔案中定義就行了。
第二種方式,不繼承TabActivity,在布局檔案中定義TabHost即可,但是TabWidget的id必須是@android:id/tabs,FrameLayout的id必須是@android:id/tabcontent。
一、繼承TabActivity
第一種繼承TabActivity,從TabActivity中用getTabHost()方法擷取TabHost。各個Tab中的内容在布局檔案中定義就行了。
1、布局檔案
打開“res/layout/activity_main.xml”檔案。
輸入以下代碼:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:id="@+id/txtnews"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/photonews"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<AnalogClock android:id="@+id/clock"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:id="@+id/videonews"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton" />
</LinearLayout>
</FrameLayout>
2、程式檔案
打開“src/com.genwoxue.switchtogglebutton/MainActivity.java”檔案。
然後輸入以下代碼:
package com.genwoxue.tabhost;
import android.app.TabActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;
public class MainActivity extends TabActivity {
//聲明TabHost
private TabHost tabNews;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//獲得目前MainActivity(繼承自TabActivity)的TabHost
tabNews = this.getTabHost();
// 通俗的說,inflate就相當于将一個xml中定義的布局找出來
LayoutInflater.from(this).inflate(R.layout.activity_main,tabNews.getTabContentView(), true);
//添加一個文本新聞選項框
tabNews.addTab(tabNews
.newTabSpec("firsttab")
.setIndicator("文本新聞",getResources().getDrawable(R.drawable.ic_launcher))
.setContent(R.id.txtnews));
//添加一個圖檔新聞選項框
tabNews.addTab(tabNews
.newTabSpec("secondtab")
.setIndicator("圖檔新聞",getResources().getDrawable(R.drawable.ic_launcher))
.setContent(R.id.photonews));
//添加一個視訊新聞選項框
tabNews.addTab(tabNews
.newTabSpec("thirdtab")
.setIndicator("視訊新聞",getResources().getDrawable(R.drawable.ic_launcher))
.setContent(R.id.videonews));
}
}
3、運作結果

二、布局檔案中定義TabHost
第二種方式,不繼承TabActivity,在布局檔案中定義TabHost即可,但是TabWidget的id必須是@android:id/tabs,FrameLayout的id必須是@android:id/tabcontent。
1、布局檔案
打開“res/layout/activity_main.xml”檔案。
輸入以下代碼:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/hometabs"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabHost android:id="@+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget android:id="@android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TabWidget>
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="第一個選項框"/>
<TextView android:id="@+id/photo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="第二個選項框"/>
<TextView android:id="@+id/video"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="第三個選項框"/>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
2、程式檔案
打開“src/com.genwoxue.tabhost/MainActivity.java”檔案。
然後輸入以下代碼:
package com.genwoxue.tabhost_b;
import android.os.Bundle;
import android.app.Activity;
import android.widget.TabHost;
import android.widget.TabWidget;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//擷取TabHost對象
TabHost tabHost = (TabHost) findViewById(R.id.tabhost);
tabHost.setup();
//擷取TabWidget對象
TabWidget tabWidget = tabHost.getTabWidget();
//添加選項框一
tabHost.addTab(tabHost
.newTabSpec("tab1")
.setIndicator("文本新聞",getResources().getDrawable(R.drawable.ic_launcher))
.setContent(R.id.text));
//添加選項框二
tabHost.addTab(tabHost
.newTabSpec("tab2")
.setIndicator("圖檔新聞",getResources().getDrawable(R.drawable.ic_launcher))
.setContent(R.id.photo));
//添加選項框三
tabHost.addTab(tabHost
.newTabSpec("tab3")
.setIndicator("視訊新聞",getResources().getDrawable(R.drawable.ic_launcher))
.setContent(R.id.video));
}
}
3、運作結果
補充說明:
Android 關于inflate
是以如果你的Activity裡如果用到别的layout,比如對話框上的layout,你還要設定對話框上的layout裡的元件(像圖檔ImageView,文字TextView)上的内容,你就必須用inflate()先将對話框上的layout找出來,然後再用這個layout對象去找到它上面的元件,如:
Viewview=View.inflate(this,R.layout.dialog_layout,null);
TextViewdialogTV=(TextView)view.findViewById(R.id.dialog_tv);
dialogTV.setText("abcd");
如果元件R.id.dialog_tv是對話框上的元件,而你直接用this.findViewById(R.id.dialog_tv)肯定會報錯.
三種方式可以生成LayoutInflater:
LayoutInflaterinflater=LayoutInflater.from(this);
LayoutInflaterinflater=getLayoutInflater();
LayoutInflaterinflater=(LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
然後調用inflate方法将xml布局檔案轉成View
publicViewinflate(intresource,ViewGrouproot,booleanattachToRoot)
在View類中,也有inflate方法
publicstaticViewinflate(Contextcontext,intresource,ViewGrouproot)