TabHost
那么我们要用到的Tab载体是TabHost,需要从TabActivity.getTabHost获取。
现在看看TabHost类,它有3个内嵌类:1个类TabHost.TabSpec,2个接口TabHost.TabContentFactory和TabHost.OnTabChangeListener。后面会介绍这些类和接口。
TabHost类的一些函数:
public void addTab (TabHost.TabSpec tabSpec) 添加tab,参数TabHost.TabSpec通过下面的函数返回得到
public TabHost.TabSpec newTabSpec (String tag) 创建TabHost.TabSpec
public void clearAllTabs () remove所有的Tabs
public int getCurrentTab ()
public String getCurrentTabTag ()
public View getCurrentTabView ()
public View getCurrentView ()
public FrameLayout getTabContentView () 返回Tab content的FrameLayout
public TabWidget getTabWidget ()
public void setCurrentTab (int index) 设置当前的Tab by index
public void setCurrentTabByTag (String tag) 设置当前的Tab by tag
public void setOnTabChangedListener (TabHost.OnTabChangeListener l) 设置TabChanged事件的响应处理
public void setup () 这个函数后面介绍
TabHost.TabSpec
从上面的函数可以知道如何添加tab了,要注意,这里的Tag(标签),不是Tab按钮上的文字。
而要设置tab的label和content,需要设置TabHost.TabSpec类。 引用SDK里面的话——“A tab has a tab indicator, content, and a tag that is used to keep track of it.”,TabHost.TabSpec就是管理这3个东西:
public String getTag ()
public TabHost.TabSpec setContent
public TabHost.TabSpec setIndicator
我理解这里的
Indicator就是Tab上的label,它可以
设置label:
setIndicator (CharSequence label)
或者同时
设置label和icon:
setIndicator (CharSequence label, Drawable icon)
或者直接
指定某个view:
setIndicator (View view)
对于
Content,就是Tab里面的内容,可以
设置View的id:
setContent(int viewId)
或者
TabHost.TabContentFactory的createTabContent(String tag)来处理:
setContent(TabHost.TabContentFactory contentFactory)
或者用
new Intent来引入其他Activity的内容:
setContent(Intent intent)
public class TabWidgetActivity extends TabActivity implements
OnTabChangeListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost TabHost = getTabHost();
TabHost.addTab(TabHost.newTabSpec("选项卡一").setIndicator("TAB 1",
getResources().getDrawable(R.drawable.ic_launcher)).setContent(
R.id.textview1));
TabHost.addTab(TabHost.newTabSpec("选项卡二").setIndicator("TAB 2",
getResources().getDrawable(R.drawable.ic_launcher)).setContent(
R.id.textview2));
TabHost.addTab(TabHost.newTabSpec("选项卡三").setIndicator("TAB 3",
getResources().getDrawable(R.drawable.ic_launcher)).setContent(
R.id.textview2));
TabHost.setCurrentTab(0);
TabHost.setOnTabChangedListener(this);
}
@Override
public void onTabChanged(String tabId) {
Dialog dialog = new AlertDialog.Builder(TabWidgetActivity.this)
.setTitle("提示").setMessage("当前选中:" + tabId + "标签")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
}).create();// 创建按钮
dialog.show();
}
}
OnTabChangeListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost TabHost = getTabHost();
TabHost.addTab(TabHost.newTabSpec("选项卡一").setIndicator("TAB 1",
getResources().getDrawable(R.drawable.ic_launcher)).setContent(
R.id.textview1));
TabHost.addTab(TabHost.newTabSpec("选项卡二").setIndicator("TAB 2",
getResources().getDrawable(R.drawable.ic_launcher)).setContent(
R.id.textview2));
TabHost.addTab(TabHost.newTabSpec("选项卡三").setIndicator("TAB 3",
getResources().getDrawable(R.drawable.ic_launcher)).setContent(
R.id.textview2));
TabHost.setCurrentTab(0);
TabHost.setOnTabChangedListener(this);
}
@Override
public void onTabChanged(String tabId) {
Dialog dialog = new AlertDialog.Builder(TabWidgetActivity.this)
.setTitle("提示").setMessage("当前选中:" + tabId + "标签")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
}).create();// 创建按钮
dialog.show();
}
}
<?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="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/textview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is a tab" />
<TextView
android:id="@+id/textview2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is another tab" />
<TextView
android:id="@+id/textview3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is a third tab" />
</FrameLayout>
</LinearLayout>
</TabHost>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/textview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is a tab" />
<TextView
android:id="@+id/textview2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is another tab" />
<TextView
android:id="@+id/textview3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is a third tab" />
</FrameLayout>
</LinearLayout>
</TabHost>