上面的是最終。
代碼結構如下。
main.xml代碼:
<?xml version="1.0" encoding="utf-8"?><br /><!-- 該布局檔案定義了標籤的內容部分,該布局檔案一定要以FrameLayout為根項目 --><br /><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"<br />android:layout_width="fill_parent" android:layout_height="fill_parent"><br /><!-- 第一個標籤內容 --><br /><LinearLayout android:id="@+id/widget_layout_Blue"<br />android:layout_width="fill_parent" android:layout_height="fill_parent"<br />android:orientation="vertical" ><br /><EditText android:id="@+id/widget34" android:layout_width="fill_parent"<br />android:layout_height="wrap_content" android:text="EditText"<br />android:textSize="18sp"><br /></EditText><br /><Button android:id="@+id/widget30" android:layout_width="wrap_content"<br />android:layout_height="wrap_content" android:text="Button"><br /></Button><br /></LinearLayout><br /><!-- 第二個標籤內容 AnalogClock為鐘錶組件--><br /><LinearLayout android:id="@+id/widget_layout_red"<br />android:layout_width="fill_parent" android:layout_height="fill_parent"<br />android:orientation="vertical" ><br /><AnalogClock android:id="@+id/widget36"<br />android:layout_width="wrap_content" android:layout_height="wrap_content"><br /></AnalogClock><br /></LinearLayout><br /><!-- 第三個標籤內容 RadioButton必須在RadioGroup中哦 --><br /><LinearLayout android:id="@+id/widget_layout_green"<br />android:layout_width="fill_parent" android:layout_height="fill_parent"<br />android:orientation="vertical"><br /><RadioGroup android:id="@+id/widget43"<br />android:layout_width="166px" android:layout_height="98px"<br />android:orientation="vertical"><br /><RadioButton android:id="@+id/widget44"<br />android:layout_width="wrap_content" android:layout_height="wrap_content"<br />android:text="RadioButton"><br /></RadioButton><br /><RadioButton android:id="@+id/widget45"<br />android:layout_width="wrap_content" android:layout_height="wrap_content"<br />android:text="RadioButton"><br /></RadioButton><br /></RadioGroup><br /></LinearLayout><br /></FrameLayout><br />
TagHostTest.java的代碼:
package cn.com.tagHost.test;</p><p>import android.app.TabActivity;<br />import android.graphics.Color;<br />import android.os.Bundle;<br />import android.view.LayoutInflater;<br />import android.view.ViewGroup;<br />import android.widget.TabHost;</p><p>public class TagHostTest extends TabActivity {</p><p>private TabHost myTabhost;</p><p>@Override<br />protected void onCreate(Bundle savedInstanceState) {<br />super.onCreate(savedInstanceState);<br />myTabhost = this.getTabHost();</p><p>/**<br /> * inflate(int resource, ViewGroup root, boolean attachToRoot)<br /> * resource 很顯然是一個資源索引id<br /> * 當attachToRoot為true時,root代表一個可放置於容器中的組件<br /> * 當attachToRoot為false時,root僅代表一個儲存值的對象<br /> * 該方法的意思是,將根據R.layout.main產生的標籤View,添加到由myTabhost.getTabContentView()獲得的父容器中<br /> * LayoutInflater類的inflate方法中有如下片段<br /> * if (root != null && attachToRoot) {<br /> root.addView(temp, params);<br /> }<br /> 其中temp是根據resource指定的資源產生的一個和標籤有關的view<br /> */<br />LayoutInflater.from(this).inflate(R.layout.main,<br />myTabhost.getTabContentView(), true);<br />myTabhost.setBackgroundColor(Color.argb(150, 22, 70, 150));</p><p>myTabhost.addTab(myTabhost.newTabSpec("One")<br />.setIndicator("A").setContent(R.id.widget_layout_Blue));</p><p>myTabhost.addTab(myTabhost.newTabSpec("Two")<br />.setIndicator("B", getResources().getDrawable(R.drawable.icon))<br />.setContent(R.id.widget_layout_green));</p><p>myTabhost.addTab(myTabhost.newTabSpec("Three")<br />.setIndicator("C", getResources().getDrawable(R.drawable.icon))<br />.setContent(R.id.widget_layout_red));<br />}<br />}
這種方法實現起來比較簡單,看看我們都做了些什麼。
第一步:定義標籤內容部分的布局檔案,該布局檔案必須以FrameLayout為根節點。
第二步:讓activity繼承TabActivity,然後實現自己的代碼。