TabHost

MainActivity.java:

package com.example.test13_tabhost;


import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;


public class MainActivity extends Activity {
private TabHost tabhost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tabhost = (TabHost) findViewById(R.id.tabhost);
tabhost.setup();//找到TabWidget和FrameLayout
//添加标签页
TabSpec tab1 = tabhost.newTabSpec("tag1");
// tab1.setIndicator("首页",getResources().getDrawable(R.drawable.i1));
tab1.setIndicator(createView("首页"));
//指定标签的内容
tab1.setContent(R.id.line1);
tabhost.addTab(tab1);
TabSpec tab2 = tabhost.newTabSpec("tag2");
// tab2.setIndicator("第二页",getResources().getDrawable(R.drawable.i2));
tab2.setIndicator(createView("第二页"));
//指定标签的内容
tab2.setContent(R.id.line2);
tabhost.addTab(tab2);
TabSpec tab3 = tabhost.newTabSpec("tag3");
// tab3.setIndicator("第三页",getResources().getDrawable(R.drawable.i7));
tab3.setIndicator(createView("第三页"));
//指定标签的内容
tab3.setContent(R.id.line3);
tabhost.addTab(tab3);
}

private View createView(String text){
View view = View.inflate(this, R.layout.tab, null);
TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
tv_title.setText(text);
return view;
}

}

activity_main.xml:

 <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:id="@+id/tabhost">
<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <TabWidget 
        android:id="@android:id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        
    </TabWidget>
    <FrameLayout 
        android:id="@android:id/tabcontent"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        >
        <!-- 首页 -->
        <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/line1"
            >
            <TextView 
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="首页"
                android:textSize="30sp"
                android:gravity="center"
                />
</LinearLayout>
 <!-- 第二页 -->
        <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/line2"
            >
            <TextView 
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="第二页"
                android:textSize="30sp"
                android:gravity="center"
                />
</LinearLayout>
 <!-- 第三页 -->
        <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/line3"
            >
            <TextView 
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="第三页"
                android:textSize="30sp"
                android:gravity="center"
                />
</LinearLayout>
   </FrameLayout>
     </LinearLayout>

</TabHost>

tab.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="@drawable/bg"
    >
    <TextView 
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="首页"
        android:textColor="@android:color/white"
        />


</LinearLayout>

TabHostTabHostTabHost