Android控件之TabHost探究

以下通过TabHost实现android选项卡。

main.xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width
="fill_parent"
android:layout_height
="fill_parent"
android:gravity
="center_horizontal"
android:orientation
="vertical">
<ImageView android:id="@+id/iv01"
android:scaleType
="fitXY"
android:layout_gravity
="center"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:src
="@drawable/andy"/>
<TextView android:id="@+id/tv01"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:textSize
="24dip"
android:text
="Android的创造者: Andy Rubin"/>
</LinearLayout>
<LinearLayout android:id="@+id/tab02"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
android:gravity
="center_horizontal"
android:orientation
="vertical">
<ImageView android:id="@+id/iv02"
android:scaleType
="fitXY"
android:layout_gravity
="center"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:src
="@drawable/bill"/>
<TextView android:id="@+id/tv02"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:textSize
="24dip"
android:text
="Java创造者之一: Bill Joy"/>
</LinearLayout>
<LinearLayout android:id="@+id/tab03"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
android:gravity
="center_horizontal"
android:orientation
="vertical">
<ImageView android:id="@+id/iv03"
android:scaleType
="fitXY"
android:layout_gravity
="center"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:src
="@drawable/torvalds"/>
<TextView android:id="@+id/tv03"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:textSize
="24dip"
android:text
="Linux之父: Linus Torvalds"/>
</LinearLayout>
</LinearLayout>

TabHostActivity类

package com.ljq.activity;

import android.app.TabActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;

public class TabHostActivity extends TabActivity{
private TabHost tab=null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

tab
=this.getTabHost();
LayoutInflater.from(
this).inflate(R.layout.main, tab.getTabContentView(), true);
tab.addTab(tab.newTabSpec(
"选项卡一").setIndicator("选项卡一",
getResources().getDrawable(R.drawable.png1)).setContent(R.id.tab01));
tab.addTab(tab.newTabSpec(
"选项卡二").setIndicator("选项卡二",
getResources().getDrawable(R.drawable.png2)).setContent(R.id.tab02));
tab.addTab(tab.newTabSpec(
"选项卡三").setIndicator("选项卡三",
getResources().getDrawable(R.drawable.png3)).setContent(R.id.tab03));
}
}

运行结果

posted on 2011-02-24 16:20  Ruthless  阅读(7436)  评论(0编辑  收藏  举报