Android---UI篇---Tab Tabhost

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


TabHost是整个Tab的容器,包括两部分,TabWidget和FrameLayout。TabWidget就是每个tab的标签,FrameLayout则是tab内容。

1、如果我们使用extends TabAcitivty,如同ListActivity,TabHost必须设置为@android:id/tabhost
2、TabWidget必须设置android:id为@android:id/tabs
3、FrameLayout需要设置android:id为@android:id/tabcontent


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


  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>  
  36. 小结:
    1、在一个tabActivity里面嵌套一个tabAcitivity, 如果在子tabActivity里面显示AlertDialog、ProgressDialog的话,就会引发此错误:android.view.WindowManager$BadTokenException: Unable to add window

    解决方法:
    可以把创建dialog时传递的参数xxx.this改成this.getParent(),其中的xxx为Activity



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值