TabHost创建的2种方式

一.如果是自定义TabHost步骤如下

1.必须给tabHost跟标签设置一个android:id="@android:id/tabhost">

2.必须创建TabWidget子节点,并且必须设定android:id="@android:id/tabs">该标签一般放到一个线性布局当中

3.必须创建FrameLayout节点,用于显示每个TabWidget标签的内容,且id必须是android:id="@android:id/tabcontent">

在代码中使用TabHost与TabActivyt比较相似,不同的只有开始的两个步骤,其具体步骤如下:

(1)使用setContentView()方法显示界面。

(2)TabHost对象获得并设置。

(3)创建并设置TabSpec对象。

(4)向TabHost中添加TabSpec完成标签页的使用。

与使用TabActivity相比较不难发现,自定义TabHost时不需要继承TabActivity了,只需要简单继承Activity就可以了,这无疑给我们编程提供了更大的自由发挥的空间。因为我们知道继承虽然会给我们的编程带来很大程度的方便,但也同样带来了很多的条条框框的限制。

案例:

public class MainActivity extends TabActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.tab);

        TabHost tab = getTabHost();
        // 也可以不要setContentView(R.layout.tab),通过下面方式也许
        // LayoutInflater.from(this).inflate(R.layout.tab,
        // tab.getTabContentView(), true);

        TabSpec tab1 = tab.newTabSpec("tab1");
        TabSpec tab2 = tab.newTabSpec("tab2");
        TabSpec tab3 = tab.newTabSpec("tab3");

        tab.addTab(tab1.setContent(R.id.tab1).setIndicator("第一个tab"));
        tab.addTab(tab2.setContent(R.id.tab2).setIndicator("第2个tab"));
        tab.addTab(tab3.setContent(R.id.tab3).setIndicator("第3个tab"));

    }
}
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        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" >
        </TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:text="第1个tab" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:text="第2个tab" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical" >

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:text="第3个tab" />
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>

</TabHost>


二.不使用Tabhost作为跟标签

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

    <LinearLayout
        android:id="@+id/sll01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/v1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:src="@drawable/t1" >
        </ImageView>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/sll02"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/v2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:src="@drawable/t2" >
        </ImageView>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/sll03"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/v3"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:src="@drawable/t3" >
        </ImageView>
    </LinearLayout>

</FrameLayout>
public class MainActivity2 extends TabActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TabHost host = getTabHost();

        LayoutInflater.from(this).inflate(R.layout.tab2,
                host.getTabContentView(), true);

        host.addTab(host.newTabSpec("tab1").setContent(R.id.sll01)
                .setIndicator("第一个", getResources().getDrawable(R.drawable.t1)));

        host.addTab(host.newTabSpec("tab2").setContent(R.id.sll02)
                .setIndicator("第二个", getResources().getDrawable(R.drawable.t2)));
        host.addTab(host.newTabSpec("tab3").setContent(R.id.sll03)
                .setIndicator("第三个", getResources().getDrawable(R.drawable.t3)));
        
        setContentView(host);
    }
}
原文地址:https://www.cnblogs.com/android-zcq/p/3140424.html