选项卡TabHost——通话记录界面
TabHost是一个简单的容器,它提供了如下两个方法来创建选项卡,添加选项卡
——newTaSpec(String tag):创建选项卡
——addTab(TabHost.TabSpec tabSpec):添加选项卡
使用TabHost的一般步骤:
1.在界面布局定义TabHost组件,并为该组件定义选项卡的内容
2.Activity应该继承TabActivity
3.调用TabActivity的getTabHost()方法获取TabHost对象
4.通过TabHost对象的方法来创建选项卡、添加选项卡
<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <!-- 定义第一个标签页的内容 --> <LinearLayout android:id="@+id/tab01" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女儿国国王 - 2010/12/12" android:textSize="11pt" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="东海龙女 - 2010/12/18" android:textSize="11pt" /> </LinearLayout> <!-- 定义第二个标签页的内容 --> <LinearLayout android:id="@+id/tab02" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="白骨精 - 2010/08/12" android:textSize="11pt" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="蜘蛛精 - 2010/09/20" android:textSize="11pt" /> </LinearLayout> <!-- 定义第三个标签页的内容 --> <LinearLayout android:id="@+id/tab03" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textSize="11pt" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="孙悟空 - 2010/09/19" android:textSize="11pt" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="猪八戒 - 2010/10/12" android:textSize="11pt" /> </LinearLayout> </TabHost>
添加三个标签页:
package org.crazyit.tabhost; import android.app.TabActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.widget.TabHost; public class TabHostTest extends TabActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TabHost tabHost = getTabHost(); //设置使用TabHost布局 LayoutInflater.from(this).inflate(R.layout.main, tabHost.getTabContentView(), true); //添加第一个标签页 tabHost.addTab(tabHost.newTabSpec("tab1") .setIndicator("已接电话") .setContent(R.id.tab01)); //添加第二个标签页 tabHost.addTab(tabHost.newTabSpec("tab2") //在标签标题上放置图标 .setIndicator("呼出电话" , getResources().getDrawable(R.drawable.icon)) .setContent(R.id.tab02)); //添加第三个标签页 tabHost.addTab(tabHost.newTabSpec("tab3") .setIndicator("未接电话") .setContent(R.id.tab03)); } }