Android中TabHost的应用

 布局文件:

<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="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >
           
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1" >

                <include
                    android:id="@+id/tab_discount"
                    layout="@layout/activity_discount" >
                </include>

                <include
                    android:id="@+id/tab_search"
                    layout="@layout/activity_search" >
                </include>

                <include
                    android:id="@+id/tab_home"
                    layout="@layout/activity_home" >
                </include>
                <include
                    android:id="@+id/tab_setting"
                    layout="@layout/activity_setting" >
                </include>


            </FrameLayout>

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="45dp"
                android:background="@drawable/base_actionbar_bg" >
            </TabWidget>
        </LinearLayout>
    </TabHost>

</LinearLayout>

代码

tabhost = getTabHost();
		tabhost.addTab(tabhost
				.newTabSpec("discount")
				.setIndicator(
						LayoutInflater.from(this).inflate(R.layout.tabitem,
								null)).setContent(R.id.tab_discount));
		tabhost.addTab(tabhost
				.newTabSpec("search")
				.setIndicator(
						LayoutInflater.from(this).inflate(R.layout.tabitem,
								null)).setContent(R.id.tab_search));
		tabhost.addTab(tabhost
				.newTabSpec("home")
				.setIndicator(
						LayoutInflater.from(this).inflate(R.layout.tabitem,
								null)).setContent(R.id.tab_home));
		tabhost.addTab(tabhost
				.newTabSpec("setting")
				.setIndicator(
						LayoutInflater.from(this).inflate(R.layout.tabitem,
								null)).setContent(R.id.tab_setting));

应注意一下内容:
  1. TabWidget很TabHost的id必须为@android :id/tabs和@android :id/tabhost
  2. LinearLayout中的Frament的weight必须上设置为1 height设置为full_parent
  3. addTab的参数为TabSpec的一个对象 tabSpec是Tabhost的一个内部类

你可能感兴趣的:(Android中TabHost的应用)