Android界面开发之切换卡TabWidget用法

来源:互联网 发布:结构体数组指针 编辑:程序博客网 时间:2024/06/05 04:55

转自:http://www.oschina.net/question/54100_32483

TabWidget类似Android中查看电话簿的界面,通过多个标签切换显示不同的内容。要实现这一效果,首先要了解TabHost ,它是一个用来存放多个Tab标签的容器。每一个Tab都可以对应自己的布局,比如,电话簿中的Tab布局就是一个List的线性布局了。

要使用TabHost ,首先需要通过getTabHost 方法来获取TabHost的对象,然后通过addTab方法来向TabHost中添加Tab。当然每个Tab在切换时都会产生一个事件,要捕捉这个事件需要设置TabActivity的事件监听沁tsetOnTabChangedListener。下面我们来看一个具体示例,运行效果如图4-65所示。当我们点击个Tab切换时,捕捉事件如图4-66所示。

首先我们来看布局中如何实现,如下代码所示。其中Tab对应的布局通过FrameLayout作为根布局,然后分别放置了3个TextView控件来显示每个Tab标签的内容。

<?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">    <LinearLayout        android:orientation="vertical"        android:layout_width="fill_parent"        android:layout_height="fill_parent">        <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>

TabWidget的使用需要继承自TabActivity类,并实现tsetOnTabChangedListener的onTabChanged方法来监听Tab的改变,如下代码所示:
package com.yarin.android.Examples_04_29; import android.app.AlertDialog;import android.app.Dialog;import android.app.TabActivity;import android.content.DialogInterface;import android.graphics.Color;import android.os.Bundle;import android.widget.TabHost;import android.widget.TabHost.OnTabChangeListener; public class Activity01 extends TabActivity{    //声明TabHost对象    TabHost mTabHost;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                 //取得TabHost对象        mTabHost = getTabHost();                 /* 为TabHost添加标签 */        //新建一个newTabSpec(newTabSpec)        //设置其标签和图标(setIndicator)        //设置内容(setContent)        mTabHost.addTab(mTabHost.newTabSpec("tab_test1")                .setIndicator("TAB 1",getResources().getDrawable(R.drawable.img1))                .setContent(R.id.textview1));        mTabHost.addTab(mTabHost.newTabSpec("tab_test2")                .setIndicator("TAB 2",getResources().getDrawable(R.drawable.img2))                .setContent(R.id.textview2));        mTabHost.addTab(mTabHost.newTabSpec("tab_test3")                .setIndicator("TAB 3",getResources().getDrawable(R.drawable.img3))                .setContent(R.id.textview3));                 //设置TabHost的背景颜色        mTabHost.setBackgroundColor(Color.argb(150, 22, 70, 150));        //设置TabHost的背景图片资源        //mTabHost.setBackgroundResource(R.drawable.bg0);                 //设置当前显示哪一个标签        mTabHost.setCurrentTab(0);                 //标签切换事件处理,setOnTabChangedListener        mTabHost.setOnTabChangedListener(new OnTabChangeListener()        {            // TODO Auto-generated method stub            @Override            public void onTabChanged(String tabId)            {                Dialog dialog = new AlertDialog.Builder(Activity01.this)                        .setTitle("提示")                        .setMessage("当前选中:"+tabId+"标签")                        .setPositiveButton("确定",                        new DialogInterface.OnClickListener()                        {                            public void onClick(DialogInterface dialog, int whichButton)                            {                                dialog.cancel();                            }                        }).create();//创建按钮                               dialog.show();            }                   });    }}

至此,Android TabWidget的用法就介绍完了,同时,也结束了这次的Android界面开发专题,谢谢阅读!

文章转载:http://www.itivy.com/android/archive/2011/11/16/android-tabwidget-usage.html


原创粉丝点击