Android之Tab分页标签的实现方法一----TabActivity和TabHost的结合(一)

许多软件,因为功能比较多,都喜欢采用Tab分页。在Android里面Tab分页,常用的方法有两种:

       1、采用TabActivity和TabHost的结合

       2、采用ActivityGroup和GridView的结合。

      这里将会一一讲到他们的实现方法。现在,先讲讲TabActivity和TabHost的结合吧。其实,TabActivity和TabHost的结合的方式有三种,如下:

        第一种方式:各个页面布局放在同一个文件,代码也紧凑一起。不建议。

        第一种方式:各个页面布局文件是分割的,但代码仍然紧凑一起,也不建议。

        第三种方式:各个页面布局文件是分割,各页面代码也是分割,建议。

        没有差,怎样体现好;这里会把三种方式一一讲述,读者自己慢慢体会。

        (一)TabActivity和TabHost的结合实现的分页标签-------方式一

         实现细节:

           1.主类继承TabActivity

           public class Pagination extends TabActivity

           2.获取当前TabHost对象

           TabHost tabHost = getTabHost();

           3.加载布局文件

           LayoutInflater.from(this).inflate(R.layout.main, tabHost.getTabContentView(), true);

           4.添加Tab分页标签

           tabHost.addTab(tabHost.newTabSpec("tab1")  
                .setIndicator("tab1", getResources().getDrawable(R.drawable.a1))  
                .setContent(R.id.view1));  

           ......... 

 

            

       

             

         1、布局文件:main.xml

        

  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. <!-- 分页一 -->  
  6. <TextView android:id="@+id/view1"  
  7. android:layout_width="fill_parent"  
  8. android:layout_height="fill_parent"  
  9. android:text="This is Tab1"/>  
  10.       
  11. <!-- 分页二 -->    
  12. <LinearLayout  
  13. android:id="@+id/view2"  
  14. android:layout_width="fill_parent"  
  15. android:layout_height="fill_parent"  
  16. android:orientation="vertical"  
  17. >  
  18. <EditText  
  19. android:id="@+id/et_text"  
  20. android:layout_width="fill_parent"  
  21. android:layout_height="wrap_content"  
  22. android:text="EditText"  
  23. android:textSize="18sp"  
  24. >  
  25. </EditText>  
  26. <Button  
  27. android:id="@+id/bt_show"  
  28. android:layout_width="149px"  
  29. android:layout_height="wrap_content"  
  30. android:text="显示"  
  31. >  
  32. </Button>  
  33. </LinearLayout>  
  34.   
  35. <!-- 分页三 -->  
  36. <TextView android:id="@+id/view3"  
  37. android:layout_width="fill_parent"  
  38. android:layout_height="fill_parent"  
  39. android:text="This is Tab3"/>  
  40.   
  41. </FrameLayout>  

         2、代码

       

  1. package com.myandroid.test;  
  2.   
  3. import android.app.TabActivity;  
  4. import android.os.Bundle;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.EditText;  
  10. import android.widget.TabHost;  
  11. import android.widget.Toast;  
  12.   
  13. public class Pagination extends TabActivity {  
  14.     private Button bt_show;  
  15.     private EditText et_text;  
  16.     /** Called when the activity is first created. */  
  17.     @Override  
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         //setContentView(R.layout.main);    //这里无需加载主页面  
  21.         TabHost tabHost = getTabHost();  
  22.         LayoutInflater.from(this).inflate(R.layout.main, //加载页面  
  23.                 tabHost.getTabContentView(), true);  
  24.         tabHost.addTab(tabHost.newTabSpec("tab1")     
  25.                 .setIndicator("tab1", getResources().getDrawable(R.drawable.a1))     
  26.                 .setContent(R.id.view1));  // 设置分页要显示的控件  
  27.         tabHost.addTab(tabHost.newTabSpec("tab3")     
  28.                 .setIndicator("tab2", getResources().getDrawable(R.drawable.a2))     
  29.                 .setContent(R.id.view2));     
  30.         tabHost.addTab(tabHost.newTabSpec("tab3")     
  31.                 .setIndicator("tab3", getResources().getDrawable(R.drawable.a3))     
  32.                 .setContent(R.id.view3));     
  33.           
  34.         bt_show = (Button)findViewById(R.id.bt_show);  
  35.         et_text = (EditText)findViewById(R.id.et_text);  
  36.         bt_show.setOnClickListener(new ClickEvent());  
  37.           
  38.     }  
  39.       
  40.     class ClickEvent implements OnClickListener{  
  41.   
  42.         @Override  
  43.         public void onClick(View v) {  
  44.             // TODO Auto-generated method stub  
  45.             Toast.makeText(Pagination.this, et_text.getText(), Toast.LENGTH_SHORT).show();  
  46.         }  
  47.           
  48.     }  
  49.       
  50.       
  51. }  

       下一篇是TabActivity和TabHost的结合实现的分页标签-------方式二


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值