Android常用高级组件之下选项卡

1.TabHost类简介

  TabHost类位于android.widget包下,是选项卡的封装类,用于创建选项卡窗口,该类的继承树如下

Java.lang.Object/Android.view.View/android.widget.FrameLayout/android.widget.TabHost

  TabHost继承自FrameLayout,是帧布局的一种,一种可以包含多个布局,然后根据用户的选择显示不同的界面。

2.选项卡使用案例

a.将需要的图片资源村房在res/drawable-mdpi目录下;

b.修改strings.xml文件,如下:

<resources>
    <string name="app_name">Sample_5_7</string>
    <string name="hello">Hello world!</string>
    <string name="andy">Android的创造者:Andy Rubin</string>
    <string name="bill">Java创造者之一:Bill Joy</string>
    <string name="linux">Linux之父:Linus Torvalds</string>
    <string name="menu_settings">Settings</string>
</resources>

c.开发main.xml文件,如下:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <LinearLayout android:id="@+id/linearLayout01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_horizontal"
        android:orientation="vertical">
        <ImageView 
            android:id="@+id/ImageView01"
            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/TextView01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="24dip"
            android:text="@string/andy"
            />
    </LinearLayout>
        <LinearLayout android:id="@+id/linearLayout02"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_horizontal"
        android:orientation="vertical">
        <ImageView 
            android:id="@+id/ImageView02"
            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/TextView02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="24dip"
            android:text="@string/bill"
            />
    </LinearLayout>
        <LinearLayout android:id="@+id/linearLayout03"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_horizontal"
        android:orientation="vertical">
        <ImageView 
            android:id="@+id/ImageView03"
            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/TextView03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="24dip"
            android:text="@string/linux"
            />
    </LinearLayout>
    
</FrameLayout>

d.开发主类Activity,如下:

package com.example.sample_5_7;

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

public class Sample_5_7 extends TabActivity 
{
    private TabHost myTabhost;//声明TabHost的引用
    public void onCreate(Bundle savedInstanceState) 
    {  
        super.onCreate(savedInstanceState);
        myTabhost = this.getTabHost();//从TabActivity上面获取放置Tab的TabHost
        LayoutInflater.from(this).inflate(R.layout.main, myTabhost.getTabContentView(),true);
        myTabhost.addTab(
                myTabhost.newTabSpec("选项卡1")
                .setIndicator("选项卡1", getResources()
                .getDrawable(R.drawable.png1))
                .setContent(R.id.linearLayout01));//设置此选项卡的布局文件
        myTabhost.addTab(
                myTabhost.newTabSpec("选项卡2")
                .setIndicator("选项卡2", getResources()
                .getDrawable(R.drawable.png2))
                .setContent(R.id.linearLayout02));//设置此选项卡的布局文件
        myTabhost.addTab(
                myTabhost.newTabSpec("选项卡3")
                .setIndicator("选项卡3", getResources()
                .getDrawable(R.drawable.png3))
                .setContent(R.id.linearLayout03));//设置此选项卡的布局文件
        
    }
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

e.运行效果图:

posted @ 2012-09-20 19:27  Edenme  阅读(222)  评论(0编辑  收藏  举报