Android学习之路十四:TabHost

  Android中的Tab选项卡,如TabHost,实现它可以继承TabActivity,当然也可以直接继承Activity。本TabActivity就是一个已经分页的Activity。

  

  案例:

  XML代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TabHost
        android:id="@+id/tabHost"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:paddingTop="80px"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/first_page"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="@string/first_page"/>
            <TextView
                android:id="@+id/second_page"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="@string/second_page"/>
        </FrameLayout>"
    </TabHost>

</LinearLayout>

   java代码:

  

first_page = (TextView) findViewById(R.id.first_page);
        second_page = (TextView) findViewById(R.id.second_page);
        TabHost th = (TabHost) findViewById(R.id.tabHost);
        TabHost.TabSpec ts = th.newTabSpec("0");
        ts.setContent(R.id.first_page);
        ts.setIndicator(this.getString(R.id.first_page));
        th.addTab(ts);
        ts = th.newTabSpec("1");
        ts.setContent(R.id.second_page);
        ts.setIndicator(this.getString(R.id.second_page));
        th.addTab(ts);
        th.setCurrentTab(0);

 

 

posted @ 2013-02-20 22:07  洒洒  阅读(196)  评论(0编辑  收藏  举报