创建Tab页面,建立可切换分页Activity

创建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>

保存,运行。

创建Tab页面,建立可切换分页Activity_第1张图片




你可能感兴趣的:(android,TabActivity)