為最終
代碼結構圖
main.xml
<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br />android:id="@+id/hometabs"<br />android:orientation="vertical"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"><br /> <!-- TabHost必須包含一個 TabWidget和一個FrameLayout--><br /> <TabHost android:id="@+id/tabhost"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> ><br /> <LinearLayout<br />android:orientation="vertical"<br />android:layout_width="fill_parent"<br />android:layout_height="fill_parent"><br /><!-- TabWidget的id屬性必須為 @android:id/tabs--><br /> <TabWidget android:id="@android:id/tabs"<br /> android:orientation="horizontal"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"><br /> </TabWidget><br /> <!-- FrameLayout的id屬性必須為 @android:id/tabcontent--><br /> <FrameLayout android:id="@android:id/tabcontent"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"><br /> <TextView android:id="@+id/view1"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"/><br /> <TextView android:id="@+id/view2"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"/><br /> <TextView android:id="@+id/view3"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"/><br /> </FrameLayout></p><p> </LinearLayout><br /> </TabHost><br /></LinearLayout><br />
java代碼如下
package cn.com.tagHost.test;</p><p>import android.app.Activity;<br />import android.os.Bundle;<br />import android.widget.TabHost;<br />import android.widget.TabWidget;</p><p>public class TagHostTest2 extends Activity {<br />@Override<br />public void onCreate(Bundle savedInstanceState) {<br />super.onCreate(savedInstanceState);<br />setContentView(R.layout.main);<br />// 擷取TabHost對象<br />TabHost tabHost = (TabHost) findViewById(R.id.tabhost);<br />// 如果沒有繼承TabActivity時,通過該種方法載入啟動tabHost<br />tabHost.setup();<br />tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("第一個標籤",<br />getResources().getDrawable(R.drawable.icon)).setContent(<br />R.id.view1));</p><p>tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("第三個標籤")<br />.setContent(R.id.view3));</p><p>tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("第二個標籤")<br />.setContent(R.id.view2));<br />}<br />}
運行得到正確的結果。
廢話連篇:這裡需要注意的是
第一:布局檔案的格式。以及TabWidget和FrameLayout的id屬性值。
第二:TabWidget代表的是標籤部分,FrameLayout代表的點擊標籤後看到的內容部分。FrameLayout裡面聲明的組件意為具備成為標籤內容的資格,具體的還要在代碼中具體指定。
你是否也想要這種結果呢。讓標籤在下部分顯示
那麼你只需要給main.xml進行下布局修改就可以了。
main.xml
<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br />android:id="@+id/hometabs" android:orientation="vertical"<br />android:layout_width="fill_parent" android:layout_height="fill_parent"><br /><!-- TabHost必須包含一個 TabWidget和一個FrameLayout--><br /><TabHost android:id="@+id/tabhost" android:layout_width="fill_parent"<br />android:layout_height="wrap_content"><br /><LinearLayout android:orientation="vertical"<br />android:layout_width="fill_parent" android:layout_height="fill_parent"></p><p><!-- FrameLayout的id屬性必須為 @android:id/tabcontent--><br /><FrameLayout android:id="@android:id/tabcontent"<br />android:layout_width="fill_parent" android:layout_height="fill_parent"><br /><TextView android:id="@+id/view1" android:layout_width="fill_parent"<br />android:layout_height="fill_parent"<br />android:text="hello baby!"<br />/><br /><TextView android:id="@+id/view2" android:layout_width="fill_parent"<br />android:layout_height="fill_parent" /><br /><TextView android:id="@+id/view3" android:layout_width="fill_parent"<br />android:layout_height="fill_parent" /><br /></FrameLayout><br /><RelativeLayout android:layout_width="fill_parent"<br />android:layout_height="fill_parent"></p><p><!-- TabWidget的id屬性必須為 @android:id/tabs--><br /><TabWidget android:id="@android:id/tabs"<br />android:orientation="horizontal" android:layout_width="fill_parent"<br />android:layout_height="wrap_content"<br />android:layout_alignParentBottom="true"<br />android:paddingBottom="0dp"<br />><br /></TabWidget><br /></RelativeLayout><br /></LinearLayout><br /></TabHost><br /></LinearLayout><br />
為了讓標籤和父容器底部持平,我們使用了android:layout_alignParentBottom="true",該屬性只有在RelativeLayout布局中才會存在哦、這也是為什麼我們將tabWidget放入一個RelativeLayout中的原因。
此外,在lineaerLayout布局中,TabWidget和FrameLayout的位置可是調換了哦。