創建Tab_Layout,繼承自TabActivity
package com.example.android_test;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
@SuppressWarnings("deprecation")
public class Tab_Layout extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_main);
//獲取該activity裏的TabHost組件
TabHost tabHost=getTabHost();
//創建Tab分頁面
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("ONE")
.setContent(new Intent(this,Layout_1.class)));
tabHost.addTab(tabHost.newTabSpec("tab2")
.setIndicator("TWO")
.setContent(new Intent(this,Layout_2.class)));
tabHost.addTab(tabHost.newTabSpec("tab3")
.setIndicator("THREE")
.setContent(new Intent(this,Layout_3.class)));
}
}
Tab_Layout佈局xml
<?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="match_parent"
android:layout_height="match_parent" >
<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" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
</TabHost>
其中三個id必須一致:android:id="@android:id/tabhost"
android:id="@android:id/tabs"
android:id="@android:id/tabcontent"
建立三個Activity和佈局文件
Layout_1、Layout_2、Layout_3
package com.example.android_test;
import android.app.Activity;
import android.os.Bundle;
public class Layout_1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_1);
}
}
layout_1.xml、layout_2.xml、layout_3.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="#234" >
</LinearLayout>
最後,在AndroidManifest.xml中註冊活動
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android_test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Tab_Layout"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Layout_1" >
</activity>
<activity
android:name=".Layout_2" >
</activity>
<activity
android:name=".Layout_3" >
</activity>
</application>
</manifest>
保存,運行。