TabWidget/TabHost的两种使用方法

来源:互联网 发布:苹果手机电子秤软件 编辑:程序博客网 时间:2024/04/29 03:45
Android TabWidget/TabHost有两种使用方法: 

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

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. <?xml version="1.0" encoding="utf-8"?>  
  2. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/TabHost01" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">  
  5.     <LinearLayout android:layout_width="fill_parent"  
  6.         android:orientation="vertical" android:layout_height="fill_parent">  
  7.         <TabWidget android:id="@android:id/tabs"  
  8.             android:layout_width="fill_parent"  
  9.             android:layout_height="wrap_content" />  
  10.         <FrameLayout android:id="@android:id/tabcontent"  
  11.             android:layout_width="fill_parent"  
  12.             android:layout_height="fill_parent">  
  13.             <LinearLayout android:id="@+id/LinearLayout1"  
  14.                 android:layout_width="fill_parent"  
  15.                 android:layout_height="wrap_content">  
  16.                 <TextView android:text="one"  
  17.                     android:id="@+id/TextView01" android:layout_width="wrap_content"  
  18.                     android:layout_height="wrap_content">  
  19.                 </TextView>  
  20.             </LinearLayout>  
  21.             <LinearLayout android:id="@+id/LinearLayout2"  
  22.                 android:layout_width="wrap_content"  
  23.                 android:layout_height="wrap_content">  
  24.                 <TextView android:text="two"  
  25.                     android:id="@+id/TextView02" android:layout_width="fill_parent"  
  26.                     android:layout_height="wrap_content">  
  27.                 </TextView>  
  28.             </LinearLayout>  
  29.             <LinearLayout android:id="@+id/LinearLayout3"  
  30.                 android:layout_width="wrap_content"  
  31.                 android:layout_height="wrap_content">  
  32.                 <TextView android:text="three"  
  33.                     android:id="@+id/TextView03" android:layout_width="fill_parent"  
  34.                     android:layout_height="wrap_content">  
  35.                 </TextView>  
  36.             </LinearLayout>  
  37.         </FrameLayout>  
  38.     </LinearLayout>  
  39. </TabHost>  

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
原创粉丝点击