D.K.的Android旅程----TabHost
很長時間沒寫部落格了,前段時間因為忙於考試,加上自己的惰性,部落格就忘寫了,今天自己補上一篇。
今天補上的這篇是關於TabHost。雖然TanHost現在已經被啟用了,但不可否認的是,在很多情況下,大多數開發者還是喜歡這個元件的。
與TabHost一起使用的還有如下元件:
·TabWidget:選項卡標籤條
·TabSpec:選項卡的一個Tab介面
TabHost是個容器,它提供了兩種方式來建立或新增選項卡:
·newTabSpec(Stringtag):新增新選項卡
·addTab(TabHost, TabSpec tabSpec):新增新選項卡
基本的介紹已經差不多了,現在上程式碼。
XML:
<?xmlversion="1.0" encoding="utf-8"?>
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
<!------------令選項卡處於螢幕下方--------------->
android:layout_alignParentBottom="true"
/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:id="@+id/tabs01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="D.K."
android:textSize="18pt"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="男"
android:textSize="18pt"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="20"
android:textSize="18pt"/>
</LinearLayout>
</FrameLayout>
</RelativeLayout>
</TabHost>
Java:
public class MainActivity extends TabActivity {
NotificationManagernm ;
@Override
public voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main) ;
TabHosttabs = getTabHost() ;
TabSpectab1 = tabs.newTabSpec("tab1").setIndicator("DK's info")
.setContent(R.id.tabs01);
tabs.addTab(tab1) ;
Intentintent = new Intent(MainActivity.this , NewClass.class) ;
TabSpectab2 = tabs.newTabSpec("tab2").setIndicator("Notify")
.setContent(intent); //想要選項卡顯示覆雜內容時,啟動一個新的Activity會//更好,在這就不寫出這個新的Activity了
tabs.addTab(tab2);
}
}