TabSpec与TabHost

Android游戏开发系统控件-TabSpec与TabHost

2012/5/12 星期六

5.12 汶川地震四周年,四年了,时间飞快,再大的苦难都属于过去,现在只着眼于眼前,把握现在,能让自己过得开心不后悔就行了。加油吧!!!

今天学习了另一个比较特殊的控件:TabSpec(分页),TabHost(分页的集合)

TabHost相当于浏览器中分页的集合,而TabSpec则相当于浏览器中的每个分页;在Android中,每一个TabSpec分页可以是一个组件,也可以是一个布局,然后将每个分页装入TabHost中,TabHost即可将其中的每个分页一并显示出来。

创建项目:TabProject

向项目资源中添加了两张图片资源:bg.png与bg2.png.

作者:wwj

功能:实现在布局中进行页面切换

项目运行结果截图:

          

 

 

 

 

修改代码:

=>>布局文件main.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical"   
  6.     android:background="@drawable/bg2">  
  7.     <Button   
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="@string/btn1"  
  11.         android:id="@+id/btn1"  
  12.         />  
  13.     <EditText   
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="@string/et1"  
  17.         android:id="@+id/et1"  
  18.         />  
  19.     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  20.         android:orientation="vertical"  
  21.         android:layout_width="fill_parent"  
  22.         android:layout_height="fill_parent"  
  23.         android:id="@+id/mylayout"  
  24.         android:background="@drawable/bg"  
  25.         >  
  26.         <Button  
  27.             android:layout_width="fill_parent"  
  28.             android:layout_height="wrap_content"  
  29.             android:text="@string/btn2"  
  30.             />  
  31.         <EditText  
  32.             android:layout_width="fill_parent"  
  33.             android:layout_height="wrap_content"  
  34.             android:text="@string/et2"  
  35.             />  
  36.     </LinearLayout>  
  37.   
  38. </LinearLayout>  

 

 

 

=>>string.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4.     <string name="hello">Hello World, TabProjectActivity!</string>  
  5.     <string name="app_name">TabProject</string>  
  6.     <string name="btn1">This is Tab1</string>  
  7.     <string name="btn2">This is Tab3</string>  
  8.     <string name="et1">This is Tab2</string>  
  9.     <string name="et2">This is Tab3</string>  
  10.   
  11. </resources>  


 

=>>TabProjectActivity.java

[java]  view plain copy
  1. package com.tabHost;  
  2.   
  3. import android.app.TabActivity;  
  4. import android.os.Bundle;  
  5. import android.view.LayoutInflater;  
  6. import android.widget.TabHost;  
  7. import android.widget.TabHost.OnTabChangeListener;  
  8. import android.widget.TabHost.TabSpec;  
  9. import android.widget.Toast;  
  10.   
  11. public class TabProjectActivity extends TabActivity implements OnTabChangeListener{  
  12.     private TabSpec ts1,ts2,ts3;    //声明3个分页  
  13.     private TabHost tableHost;      //分页菜单(tab容器)  
  14.     /** Called when the activity is first created. */  
  15.     @Override  
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         tableHost = this.getTabHost();//实例(分页)菜单  
  19.         //利用LayoutInflater将布局与分页菜单一起显示  
  20.         LayoutInflater.from(this).inflate(R.layout.main, tableHost.getTabContentView());  
  21.         ts1 = tableHost.newTabSpec("tabOne");//实例化一个分页  
  22.         ts1.setIndicator("Tab1");//设置此页显示的标题  
  23.         ts1.setContent(R.id.btn1);//设置此分页的资源id  
  24.         ts2 = tableHost.newTabSpec("tabTwo");  
  25.         //设置此分页显示的标题和图标  
  26.         ts2.setIndicator("Tab2",getResources().getDrawable(R.drawable.ic_launcher));  
  27.         ts2.setContent(R.id.et1);  
  28.         ts3 = tableHost.newTabSpec("tavThree");  
  29.         ts3.setIndicator("Tab3");  
  30.         ts3.setContent(R.id.mylayout);//设置此分页的布局id  
  31.         tableHost.addTab(ts1);//菜单中添加ts1分页  
  32.         tableHost.addTab(ts2);  
  33.         tableHost.addTab(ts3);  
  34.         tableHost.setOnTabChangedListener(this);//这次监听器        
  35.         }  
  36.         public void onTabChanged(String tabId){  
  37.             if(tabId.equals("tabOne")){  
  38.                 Toast.makeText(this"分页1", Toast.LENGTH_LONG).show();  
  39.             }  
  40.             if(tabId.equals("tabTwo")){  
  41.                 Toast.makeText(this"分页2", Toast.LENGTH_LONG).show();  
  42.             }  
  43.             if(tabId.equals("tabThree")){  
  44.                 Toast.makeText(this,"分页3",Toast.LENGTH_LONG).show();  
  45.             }  
  46.         }  
  47.           
  48. }  


 

自动添加的资源文件R.java

[java]  view plain copy
  1. /* AUTO-GENERATED FILE.  DO NOT MODIFY. 
  2.  * 
  3.  * This class was automatically generated by the 
  4.  * aapt tool from the resource data it found.  It 
  5.  * should not be modified by hand. 
  6.  */  
  7.   
  8. package com.tabHost;  
  9.   
  10. public final class R {  
  11.     public static final class attr {  
  12.     }  
  13.     public static final class drawable {  
  14.         public static final int bg=0x7f020000;  
  15.         public static final int bg2=0x7f020001;  
  16.         public static final int ic_launcher=0x7f020002;  
  17.     }  
  18.     public static final class id {  
  19.         public static final int btn1=0x7f050000;  
  20.         public static final int et1=0x7f050001;  
  21.         public static final int mylayout=0x7f050002;  
  22.     }  
  23.     public static final class layout {  
  24.         public static final int main=0x7f030000;  
  25.     }  
  26.     public static final class string {  
  27.         public static final int app_name=0x7f040001;  
  28.         public static final int btn1=0x7f040002;  
  29.         public static final int btn2=0x7f040003;  
  30.         public static final int et1=0x7f040004;  
  31.         public static final int et2=0x7f040005;  
  32.         public static final int hello=0x7f040000;  
  33.     }  
  34. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值