TabWidget切换卡

Tab选项卡类似与电话本的界面,通过多个标签切换不同的内容,要实现这个效果,首先要知道TabHost,它是一个用来存放多个Tab标签的容器,每一个Tab都可以对应自己的布局,比如,电话本中的Tab布局就是一个线性布局

要使用TabHost,首先要通过getTabHost方法获取TabHost的对象,然后通过addTab方法来向TabHost中添加Tab,当然每个Tab在切换时都会产生一个事件,要捕捉这个事件,需要设置TabActivity的事件监听setOnTabChangedListener

下面是个小例子:

Java代码
  1. TabTest.java   
  2.   
  3. view plaincopy to clipboardprint?   
  4. package org.hualang.tab;     
  5. import android.app.Activity;     
  6. import android.app.TabActivity;     
  7. import android.graphics.Color;     
  8. import android.os.Bundle;     
  9. import android.widget.TabHost;     
  10. import android.widget.Toast;     
  11. import android.widget.TabHost.OnTabChangeListener;     
  12. public class TabTest extends TabActivity {     
  13.     /** Called when the activity is first created. */     
  14.     TabHost tabhost;     
  15.     @Override     
  16.     public void onCreate(Bundle savedInstanceState) {     
  17.         super.onCreate(savedInstanceState);     
  18.         setContentView(R.layout.main);     
  19.         //取得TabHost对象     
  20.         tabhost = getTabHost();     
  21.         //为TabHost添加标签     
  22.         //新建一个newTabSpec(newTabSpec)     
  23.         //设置其标签和图标(setIndicator)     
  24.         //设置内容(setContent)     
  25.         tabhost.addTab(tabhost.newTabSpec("tab1")     
  26.                 .setIndicator("TAB 1",getResources().getDrawable(R.drawable.img1))     
  27.                 .setContent(R.id.text1));     
  28.         tabhost.addTab(tabhost.newTabSpec("tab2")     
  29.                 .setIndicator("TAB 2",getResources().getDrawable(R.drawable.img2))     
  30.                 .setContent(R.id.text2));     
  31.         tabhost.addTab(tabhost.newTabSpec("tab3")     
  32.                 .setIndicator("TAB 3",getResources().getDrawable(R.drawable.img3))     
  33.                 .setContent(R.id.text3));     
  34.         //设置TabHost的背景颜色     
  35.         //tabhost.setBackgroundColor(Color.argb(150,22,70,150));     
  36.         //设置TabHost的背景图片资源     
  37.         tabhost.setBackgroundResource(R.drawable.bg0);     
  38.         //设置当前显示哪个标签     
  39.         tabhost.setCurrentTab(0);     
  40.         //标签切换事件处理,setOnTabChangedListener     
  41.         tabhost.setOnTabChangedListener(new OnTabChangeListener()     
  42.         {     
  43.             public void onTabChanged(String tabId)     
  44.             {     
  45.                 Toast toast=Toast.makeText(getApplicationContext(), "现在是"+tabId+"标签", Toast.LENGTH_SHORT);     
  46.                 toast.show();     
  47.             }     
  48.         });     
  49.              
  50.     }     
  51. }     
  52.   

 

main.xml

XML/HTML代码
  1. <?xml version="1.0" encoding="utf-8"?>     
  2. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"     
  3.     android:id="@android:id/tabhost"     
  4.     android:layout_width="fill_parent"     
  5.     android:layout_height="fill_parent">     
  6.     <LinearLayout     
  7.         android:orientation="vertical"     
  8.         android:layout_width="fill_parent"     
  9.         android:layout_height="fill_parent">     
  10.         <TabWidget     
  11.             android:id="@android:id/tabs"     
  12.             android:layout_width="fill_parent"     
  13.             android:layout_height="wrap_content" />     
  14.         <FrameLayout     
  15.             android:id="@android:id/tabcontent"     
  16.             android:layout_width="fill_parent"     
  17.             android:layout_height="fill_parent">     
  18.             <TextView      
  19.                 android:id="@+id/text1"     
  20.                 android:layout_width="fill_parent"     
  21.                 android:layout_height="fill_parent"      
  22.                 android:text="选项卡1" />     
  23.             <TextView      
  24.                 android:id="@+id/text2"     
  25.                 android:layout_width="fill_parent"     
  26.                 android:layout_height="fill_parent"      
  27.                 android:text="选项卡2" />     
  28.             <TextView      
  29.                 android:id="@+id/text3"     
  30.                 android:layout_width="fill_parent"     
  31.                 android:layout_height="fill_parent"      
  32.                 android:text="选项卡3" />     
  33.         </FrameLayout>     
  34.     </LinearLayout>     
  35. </TabHost>    



 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值