选项卡是通过TabHost和TabActivity一起实现的,TabHost是Android中很常用的布局之一,它的标签可以有文本和文本图片样式。点击不同标签还可以切换标签。TabHost类的继承图如下:
java.lang.Object
android.view.View
android.view.ViewGroup
android.widget.FrameLayout
android.widget.TabHost
android.widget.TabHost继承了android.widget.FrameLayout框架布局类。下面是一个文本图片选项卡例子,如图所示。
代码请参考代码清单7-11,完整代码请参考chapter7_1工程中Tab1代码部分。
【代码清单7-11】
01
public class Tab1 extends TabActivity {
02
@Override
03
public void onCreate(Bundle savedInstanceState) {
04
super.onCreate(savedInstanceState);
05
TabHost tabHost = getTabHost();
06
LayoutInflater.from(this).inflate(R.layout.tab1_layout,
07
tabHost.getTabContentView(), true);
08
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("tab1",
09
getResources().getDrawable(R.drawable.redimage)).setContent(
10
R.id.view1));
11
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("tab2",
12
getResources().getDrawable(R.drawable.yellowimage)).setContent(
13
R.id.view2));
14
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("tab3")
15
.setContent(R.id.view3));
16
}
17
}
选项卡屏幕的Activity必须继承TabActivity,通过基类TabActivity提供的方法getTabHost()可以获得TabHost对象。下面的代码实现了为TabHost指定布局文件:LayoutInflater.from(this).inflate(R.layout.tab1_layout,tabHost.getTabContentView(), true);addTab(TabHost.TabSpec tabSpec)方法可以添加选项卡的标签,本例中有三个标签。TabHost.TabSpec调用setIndicator()设置标签样式,有三个setIndicator ()方法: setIndicator(CharSequence label) 指定标签的文本信息; setIndicator(CharSequence label, Drawable icon) 指定文本图片标签; setIndicator(View view) 使用一个View指定标签。TabHost.TabSpec调用setContent ()设置各个选项卡容纳的内容,有三个setContent ()方法:setContent(TabHost.TabContentFactory contentFactory) 通过TabHost.TabContentFactory工厂类创建选项卡的内容;setContent(int viewId) 通过一个id指定选项卡内容; setContent(Intent intent) 通过一个Intent指定选择选项卡跳转到一个Activity。布局文件请参考代码清单7-12,完整代码请参考chapter7_1工程中tab1_layout.xml代码部分(chapter7_1/res/layout/tab1_layout.xml)。【代码清单7-12】
01
<?xml version="1.0" encoding="utf-8"?>
02
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03
android:orientation="vertical"
04
android:layout_width="fill_parent"
05
android:layout_height="fill_parent"
06
>
07
<TextView android:id="@+id/view1"
08
android:layout_width="match_parent"
09
android:layout_height="match_parent"
10
android:text="@string/tab1"/>
11
<TextView android:id="@+id/view2"
12
android:layout_width="match_parent"
13
android:layout_height="match_parent"
14
android:text="@string/tab2"/>
15
<TextView android:id="@+id/view3"
16
android:layout_width="match_parent"
17
android:layout_height="match_parent"
18
android:text="@string/tab3"/>
19
</LinearLayout>
下面的例子是指定一个Intent的选项卡跳转到一个Activity的例子,如图所示,有两个选项卡:城市列表和城市展示。 代码请参考代码清单7-13,完整代码请参考chapter7_1工程中Tab2代码部分。【代码清单7-13】
01
public class Tab2 extends TabActivity {
02
@Override
03
protected void onCreate(Bundle savedInstanceState) {
04
super.onCreate(savedInstanceState);
05
TabHost tabHost = getTabHost();
06
tabHost.addTab(tabHost。newTabSpec("tab1").setIndicator("城市列表")
07
.setContent(new Intent(this, ListView_1.class)));
08
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("城市展示")
09
.setContent(
10
new Intent(this, ListViewIcon_3.class)
11
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
12
}
13
}
通过setContent方法设置Intent,当用户点击了选项卡的标签后,选项卡的内容会跳转。跳转之后的ListView_1和ListViewIcon_3是两个ListView展示城市信息,在这里我们就不多介绍了。