1.第一种方式 继承于TabActivity 这种SDK 4.1以后已经废弃了
package com.halleluja.tab2; import android.app.TabActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.widget.TabHost; import android.widget.TabHost.OnTabChangeListener; public class Tab2DemoActivity extends TabActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TabHost tabHost = this.getTabHost(); LayoutInflater inflater = LayoutInflater.from(this); inflater.inflate(R.layout.main, tabHost.getTabContentView()); tabHost.addTab(tabHost.newTabSpec("a").setIndicator("AA").setContent(R.id.tv1)); tabHost.addTab(tabHost.newTabSpec("b").setIndicator("BB").setContent(R.id.tv2)); tabHost.setOnTabChangedListener(listener); } OnTabChangeListener listener = new OnTabChangeListener() { public void onTabChanged(String tabId) { System.out.println(tabId); } }; }
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/lay" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="11111111111" android:id="@+id/tv1" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="222222222" android:id="@+id/tv2" /> </LinearLayout>
第二种方法:使用布局文件
package com.halleluja.tab; import android.app.Activity; import android.os.Bundle; import android.view.LayoutInflater; import android.widget.TabHost; public class TabDemoActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TabHost tabHost = (TabHost)findViewById(R.id.tabhost); tabHost.setup(); LayoutInflater inflater_tab1 = LayoutInflater.from(this); inflater_tab1.inflate(R.layout.a, tabHost.getTabContentView()); inflater_tab1.inflate(R.layout.b, tabHost.getTabContentView()); tabHost.addTab(tabHost.newTabSpec("aa").setIndicator("111").setContent(R.id.lay1)); tabHost.addTab(tabHost.newTabSpec("bb").setIndicator("222").setContent(R.id.lay2)); } } package com.halleluja.tab; import android.app.TabActivity; import android.os.Bundle; public class AActivity extends TabActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.a); } } package com.halleluja.tab; import android.app.TabActivity; import android.os.Bundle; public class BActivity extends TabActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.b); } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dp"> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="5dp" android:layout_weight="1" /> </LinearLayout> </TabHost>
a.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/lay1" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="aaaaaaaaaaaaaa" /> </LinearLayout>
b.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/lay2" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="bbbbbbbbbbbbb" /> </LinearLayout>
如果想要把标签放在底部只要将main.xml修改成:
<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dp"> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="5dp" android:layout_weight="1" /> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0" /> </LinearLayout> </TabHost>