使用TabHost有两种方法,一种是继承TabActivity;一种是不继承TabActivity。
第一种:继承TabActivity
1、如果我们使用extendsTabAcitivty,如同ListActivity,TabHost必须设置为@android :id/tabhost
2、TabWidget必须设置android:id为@android :id/tabs
3、FrameLayout需要设置android:id为@android :id/tabcontent
main.xml示例:
<?xml version="1.0" encoding="UTF-8"?>
<TabHost
android:id="@android :id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="@android :id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="1.0" />
<TabWidget android:id="@android:id/tabs"
android:visibility="gone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.0" />
<RadioGroup
android:gravity="center_vertical"
android:layout_gravity="bottom"
android:orientation="horizontal"
android:id="@id/main_radio"
android:background="@drawable/maintab_toolbar_bg"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@id/radio_button0"
android:layout_marginTop="2.0dip"
android:text="@string/main_home"
android:drawableTop="@drawable/icon_1_n"
style="@style/main_tab_bottom" />
</RadioGroup>
</LinearLayout>
</TabHost>
TabActivity 相关函数如下:
public TabWidget getTabWidget () 获得当前TabActivity 的TabWidget
public TabHost getTabHost () 获得当前TabActivity的TabHost
TabHost:
现在看看TabHost类,它有3个内嵌类:1个类TabHost.TabSpec,2个接口 TabHost.TabContentFactory和TabHost.OnTabChangeListener。
public void addTab (TabHost.TabSpec tabSpec) 添加 tab,参数TabHost.TabSpec通过下面的函数返回得到
TabHost.TabSpec
package com.yang.tabletest;
import android.app.TabActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;
public class TableTestAcitivity extends TabActivity{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
//获得当前TabActivity的TabHost
TabHost tabHost = getTabHost();
LayoutInflater.from(this).inflate(R.layout.tabs1, tabHost.getTabContentView(), true);
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("主页")
.setContent(R.id.view1));
tabHost.addTab(tabHost.newTabSpec("tab2")
.setIndicator("标题")
.setContent(R.id.view2));
tabHost.addTab(tabHost.newTabSpec("tab3")
.setIndicator("简介")
.setContent(R.id.view3));
tabHost.addTab(tabHost.newTabSpec("tab4")
.setIndicator("关于")
.setContent(R.id.view4));
}
}
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/view1"
android:background="@drawable/blue"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/tabs_1_tab_1"/>
<TextView android:id="@+id/view2"
android:background="@drawable/red"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/tabs_1_tab_2"/>
<TextView android:id="@+id/view3"
android:background="@drawable/green"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/tabs_1_tab_3"/>
<TextView android:id="@+id/view4"
android:background="@drawable/green"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/tabs_1_tab_4"/>
</FrameLayout>
string.xml的代码
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, TableTestAcitivity!</string>
<string name="app_name">阿福学习</string>
<string name="tabs_1_tab_1">主页</string>
<string name="tabs_1_tab_2">标题</string>
<string name="tabs_1_tab_3">关于</string>
<string name="tabs_1_tab_4">返回</string>
</resources>
color.xml代码
<?xml version="1.0" encoding="utf-8"?>第二个例子代码:
package com.yang.tabletest;
import android.app.TabActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TabHost;
import android.widget.TextView;
public class TableTestAcitivity extends TabActivity implements TabHost.TabContentFactory{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("主页", getResources().getDrawable(R.drawable.test))
.setContent(this));
tabHost.addTab(tabHost.newTabSpec("tab2")
.setIndicator("标题",getResources().getDrawable(R.drawable.test))
.setContent(this));
tabHost.addTab(tabHost.newTabSpec("tab3")
.setIndicator("关于",getResources().getDrawable(R.drawable.test))
.setContent(this));
}
@Override
public View createTabContent(String arg0) {
final TextView tv = new TextView(this);
tv.setText("Content for tab with tag " + arg0);
return tv;
}
}
原文:http://yangguangfu.iteye.com/blog/679001