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

來源:互聯網
上載者:User

上面的是最終。

代碼結構如下。

 

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,然後實現自己的代碼。

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.