Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1536016
  • 博文数量: 113
  • 博客积分: 3526
  • 博客等级: 中校
  • 技术积分: 1815
  • 用 户 组: 普通用户
  • 注册时间: 2009-09-08 09:46
个人简介

记录总结自己的工作

文章分类

全部博文(113)

文章存档

2015年(19)

2014年(10)

2013年(6)

2012年(16)

2011年(24)

2010年(21)

2009年(17)

分类: Android平台

2013-02-22 11:41:23

Android TabWidget/TabHost有两种使用方法:

第一种:使用系统自带写好的TabHost(及继承自TabActivity类)具体代码如下:
Java代码 复制代码 收藏代码
  1. "1.0" encoding="utf-8"?>   
  2. ""  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">   
  5.     "@+id/tab1"  
  6.         android:layout_width="fill_parent" android:layout_height="fill_parent"  
  7.         androidrientation="vertical">   
  8.         "@+id/TextView1"  
  9.             android:text="This is a tab1" android:layout_width="fill_parent"  
  10.             android:layout_height="wrap_content">   
  11.            
  12.        
  13.     "@+id/tab2"  
  14.         android:layout_width="fill_parent" android:layout_height="fill_parent"  
  15.         androidrientation="vertical">   
  16.         "@+id/TextView2"  
  17.             android:text="This is a tab2" android:layout_width="fill_parent"  
  18.             android:layout_height="wrap_content">   
  19.            
  20.        
  21.     "@+id/tab3"  
  22.         android:layout_width="fill_parent" android:layout_height="fill_parent"  
  23.         androidrientation="vertical">   
  24.         "@+id/TextView3"  
  25.             android:text="This is a tab3" android:layout_width="fill_parent"  
  26.             android:layout_height="wrap_content">   
  27.            
  28.        
  29.   

Java代码 复制代码 收藏代码
  1. package com.Aina.Android;   
  2.   
  3. import android.app.AlertDialog;   
  4. import android.app.Dialog;   
  5. import android.app.TabActivity;   
  6. import android.content.DialogInterface;   
  7. import android.os.Bundle;   
  8. import android.view.LayoutInflater;   
  9. import android.widget.TabHost;   
  10.   
  11. public class Test_TabWidget extends TabActivity {   
  12.     /** Called when the activity is first created. */  
  13.     private TabHost tabHost;   
  14.   
  15.     @Override  
  16.     public void onCreate(Bundle savedInstanceState) {   
  17.         super.onCreate(savedInstanceState);   
  18.         // setContentView(R.layout.main);   
  19.         tabHost = this.getTabHost();   
  20.         LayoutInflater li = LayoutInflater.from(this);   
  21.         li.inflate(R.layout.main, tabHost.getTabContentView(), true);   
  22.         tabHost.addTab(tabHost.newTabSpec("Tab_1").setContent(R.id.tab1)   
  23.                 .setIndicator("TAB1",   
  24.                         this.getResources().getDrawable(R.drawable.img1)));   
  25.         tabHost.addTab(tabHost.newTabSpec("Tab_2").setContent(R.id.tab2)   
  26.                 .setIndicator("TAB2",   
  27.                         this.getResources().getDrawable(R.drawable.img2)));   
  28.         tabHost.addTab(tabHost.newTabSpec("Tab_3").setContent(R.id.tab3)   
  29.                 .setIndicator("TAB3",   
  30.                         this.getResources().getDrawable(R.drawable.img3)));   
  31.         tabHost.setCurrentTab(1);   
  32. //      tabHost.setBackgroundColor(Color.GRAY);   
  33.         tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {   
  34.   
  35.             public void onTabChanged(String tabId) {   
  36.                 Dialog dialog = new AlertDialog.Builder(Test_TabWidget.this)   
  37.                         .setTitle("提示").setMessage(   
  38.                                 "选中了" + tabId + "选项卡").setIcon(R.drawable.icon).setPositiveButton("确定"new DialogInterface.OnClickListener(){   
  39.   
  40.                                     public void onClick(DialogInterface dialog,   
  41.                                             int which) {   
  42.                                         // TODO Auto-generated method stub   
  43.                                            
  44.                                     }   
  45.                                        
  46.                                 }).create();   
  47.                 dialog.show();   
  48.   
  49.             }   
  50.   
  51.         });   
  52.     }   
  53. }  



第二种:就是定义我们自己的tabHost:不用继承TabActivity,具体代码如下:
Java代码 复制代码 收藏代码
  1. "1.0" encoding="utf-8"?>   
  2. ""  
  3.     android:id="@+id/TabHost01" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">   
  5.     "fill_parent"  
  6.         android:orientation="vertical" android:layout_height="fill_parent">   
  7.         "@android:id/tabs"  
  8.             android:layout_width="fill_parent"  
  9.             android:layout_height="wrap_content" />   
  10.         "@android:id/tabcontent"  
  11.             android:layout_width="fill_parent"  
  12.             android:layout_height="fill_parent">   
  13.             "@+id/LinearLayout1"  
  14.                 android:layout_width="fill_parent"  
  15.                 android:layout_height="wrap_content">   
  16.                 "one"  
  17.                     android:id="@+id/TextView01" android:layout_width="wrap_content"  
  18.                     android:layout_height="wrap_content">   
  19.                    
  20.                
  21.             "@+id/LinearLayout2"  
  22.                 android:layout_width="wrap_content"  
  23.                 android:layout_height="wrap_content">   
  24.                 "two"  
  25.                     android:id="@+id/TextView02" android:layout_width="fill_parent"  
  26.                     android:layout_height="wrap_content">   
  27.                    
  28.                
  29.             "@+id/LinearLayout3"  
  30.                 android:layout_width="wrap_content"  
  31.                 android:layout_height="wrap_content">   
  32.                 "three"  
  33.                     android:id="@+id/TextView03" android:layout_width="fill_parent"  
  34.                     android:layout_height="wrap_content">   
  35.                    
  36.                
  37.            
  38.        
  39.   

Java代码 复制代码 收藏代码
  1. package com.Aina.Android;   
  2.   
  3. import android.app.Activity;   
  4. import android.os.Bundle;   
  5. import android.util.Log;   
  6. import android.view.LayoutInflater;   
  7. import android.widget.TabHost;   
  8.   
  9. public class Test_TabHost extends Activity {   
  10.     /** Called when the activity is first created. */  
  11.     private TabHost tabHost;   
  12.   
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {   
  15.         super.onCreate(savedInstanceState);   
  16.         setContentView(R.layout.main);   
  17.         try{   
  18.             tabHost = (TabHost) this.findViewById(R.id.TabHost01);   
  19.             tabHost.setup();   
  20.                
  21.             tabHost.addTab(tabHost.newTabSpec("tab_1")   
  22.                     .setContent(R.id.LinearLayout1)   
  23.                     .setIndicator("TAB1",this.getResources().getDrawable(R.drawable.img1)));   
  24.             tabHost.addTab(tabHost.newTabSpec("tab_2")   
  25.                     .setContent(R.id.LinearLayout2).setIndicator("TAB2",   
  26.                             this.getResources().getDrawable(R.drawable.img2)));   
  27.             tabHost.addTab(tabHost.newTabSpec("tab_3")   
  28.                     .setContent(R.id.LinearLayout3).setIndicator("TAB3",   
  29.                             this.getResources().getDrawable(R.drawable.img3)));   
  30.             tabHost.setCurrentTab(1);   
  31.         }catch(Exception ex){   
  32.             ex.printStackTrace();   
  33.             Log.d("EXCEPTION", ex.getMessage());   
  34.         }   
  35.   
  36.     }   
  37. }  


注意:第二种方法时布局文件中的TabWidget的id必须定义为:android:id="@android:id/tabs",FrameLayout的id必须定义为:android:id="@android:id/tabcontent" 其它控件没有限制,否则报错。


【Android进阶】嵌套TabHost (TabHost中放TabHost,类似二级目录、二级树)
http://blog.csdn.net/feng88724/archive/2011/02/23/6203358.aspx
 
 
阅读(11948) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~