【Android】自訂 Tabhost

來源:互聯網
上載者:User

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>

相關文章

聯繫我們

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