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

来源:互联网 发布:java object转换成date 编辑:程序博客网 时间:2024/06/07 03:47

创建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 {@Overrideprotected 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 {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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>

保存,运行。





0 0
原创粉丝点击