android tabHost布局之一 不繼承TabActivity並以布局檔案進行布局

來源:互聯網
上載者:User

為最終

代碼結構圖

 

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的位置可是調換了哦。

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.