:
MainActivity.javapackage tianshuai.homePage;</p><p>import android.app.TabActivity;<br />import android.os.Bundle;<br />import android.view.LayoutInflater;<br />import android.widget.RelativeLayout;<br />import android.widget.TabHost;<br />import android.widget.TabHost.TabSpec;<br />import android.widget.TextView;</p><p>public class MainActivity extends TabActivity<br />{<br />/** Called when the activity is first created. */<br />@Override<br />public void onCreate(Bundle savedInstanceState)<br />{<br />super.onCreate(savedInstanceState);</p><p>//取得當前的TabHost<br />TabHost tabs = getTabHost();</p><p>//載入主布局檔案<br />LayoutInflater.from(this).inflate(R.layout.main, tabs.getTabContentView(), true);</p><p>//給Tab1添加自訂樣式<br />RelativeLayout tabStyle1 = (RelativeLayout)LayoutInflater.from(this).inflate(R.layout.tab_style, null);<br />TextView text1 = (TextView)tabStyle1.findViewById(R.id.tab_label);<br />text1.setText("1");</p><p>//給Tab2添加自訂樣式<br />RelativeLayout tabStyle2 = (RelativeLayout)LayoutInflater.from(this).inflate(R.layout.tab_style, null);<br />TextView text2 = (TextView)tabStyle2.findViewById(R.id.tab_label);<br />text2.setText("2");</p><p>//建立新Tab, 使用tab1的樣式<br />TabSpec tab1 = tabs.newTabSpec("tab1");<br />tab1.setIndicator(tabStyle1);<br />tab1.setContent(R.id.Tab1);<br />tabs.addTab(tab1);</p><p>//建立新Tab, 使用tab2的樣式<br />TabSpec tab2 = tabs.newTabSpec("tab2");<br />tab2.setIndicator(tabStyle2);<br />tab2.setContent(R.id.Tab2);<br />tabs.addTab(tab2);<br />}</p><p>}main.xml<?xml version="1.0" encoding="utf-8"?><br /><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"<br />android:layout_width="fill_parent"<br />android:layout_height="fill_parent"<br /> ></p><p> <LinearLayout<br /> android:id="@+id/Tab1"<br /> android:layout_width="wrap_content"<br /> android:layout_height="fill_parent"<br /> android:orientation="vertical"<br /> ></p><p> <TextView<br />android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:text="這是tab1"<br /> /><br /> </LinearLayout></p><p> <LinearLayout<br /> android:id="@+id/Tab2"<br /> android:layout_width="wrap_content"<br /> android:layout_height="fill_parent"<br /> android:orientation="vertical"<br /> ></p><p> <TextView<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:text="這是tab2"<br /> /><br /> </LinearLayout><br /></FrameLayout>
table_style.xml<?xml version="1.0" encoding="UTF-8"?><br /><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> android:paddingLeft="5dip"<br /> android:paddingRight="5dip"</p><p> > </p><p> <TextView android:id="@+id/tab_label"<br /> android:layout_width="fill_parent"<br /> android:layout_height="30px"<br /> android:gravity="center"<br /> android:textColor="#000000"<br /> android:textStyle="bold"<br /> android:background="@drawable/tab"<br /> /><br /></RelativeLayout>tab.xml<?xml version="1.0" encoding="UTF-8"?><br /><selector xmlns:android = "http://schemas.android.com/apk/res/android"> </p><p> <item<br /> android:state_selected = "true"<br /> android:drawable = "@drawable/red"<br /> /><br /> <item<br /> android:state_selected = "false"<br /> android:drawable = "@drawable/bule"<br /> /><br /></selector>