RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
Android中怎么实现选项卡功能

本篇文章为大家展示了Android中怎么实现选项卡功能,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

赣州ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18982081108(备注:SSL证书合作)期待与您的合作!

首先创建Android工程命名自己的Activity为HelloTabWidget

在main.xml或者自己定义的*.xml文件中创建一个TabHost,需要两个元素TabWidget和FrameLayout 通常会把这两个元素放到LinearLayout中。FrameLayout作为改变内容content用的。
注意:TabWidget和FrameLayout 有不同的ID命名空间android:id="@android:id/idnames",这个是必须的因此TabHost才能自动找到它,Activity需要继承TabActivity。

Android选项卡Xml代码

  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/textview1"   

  20. android:layout_width="fill_parent"   

  21. android:layout_height="fill_parent"   

  22. android:text="this is a tab" />   

  23. < TextView   

  24. android:id="@+id/textview2"   

  25. android:layout_width="fill_parent"   

  26. android:layout_height="fill_parent"   

  27. android:text="this is another tab" />   

  28. < TextView   

  29. android:id="@+id/textview3"   

  30. android:layout_width="fill_parent"   

  31. android:layout_height="fill_parent"   

  32. android:text="this is a third tab" />   

  33. < /FrameLayout>   

  34. < /LinearLayout>   

  35. < /TabHost>   

  36.  

  37. < ?xml version="1.0" encoding="utf-8"?> 

  38. < TabHost xmlns:android=
    "http://schemas.android.com/apk/res/android" 

  39. android:id="@android:id/tabhost" 

  40. android:layout_width="fill_parent" 

  41. android:layout_height="fill_parent"> 

  42. < LinearLayout 

  43. android:orientation="vertical" 

  44. android:layout_width="fill_parent" 

  45. android:layout_height="fill_parent"> 

  46. < TabWidget 

  47. android:id="@android:id/tabs" 

  48. android:layout_width="fill_parent" 

  49. android:layout_height="wrap_content" /> 

  50. < FrameLayout 

  51. android:id="@android:id/tabcontent" 

  52. android:layout_width="fill_parent" 

  53. android:layout_height="fill_parent"> 

  54. < TextView   

  55. android:id="@+id/textview1" 

  56. android:layout_width="fill_parent" 

  57. android:layout_height="fill_parent"   

  58. android:text="this is a tab" /> 

  59. < TextView   

  60. android:id="@+id/textview2" 

  61. android:layout_width="fill_parent" 

  62. android:layout_height="fill_parent"   

  63. android:text="this is another tab" /> 

  64. < TextView   

  65. android:id="@+id/textview3" 

  66. android:layout_width="fill_parent" 

  67. android:layout_height="fill_parent"   

  68. android:text="this is a third tab" /> 

  69. < /FrameLayout> 

  70. < /LinearLayout> 

  71. < /TabHost>  

Activity需要继承TabActivity

Android选项卡Java代码

public class HelloTabWidget extends TabActivity   public class HelloTabWidget extends TabActivity

Android选项卡Java代码

  1. public void onCreate(Bundle savedInstanceState) {   

  2. super.onCreate(savedInstanceState);   

  3. setContentView(R.layout.main);   

  4. mTabHost = getTabHost();   

  5. mTabHost.addTab(mTabHost.newTabSpec("tab_test1").
    setIndicator("TAB 1").setContent(R.id.textview1));   

  6. mTabHost.addTab(mTabHost.newTabSpec("tab_test2").
    setIndicator("TAB 2").setContent(R.id.textview2));   

  7. mTabHost.addTab(mTabHost.newTabSpec("tab_test3").
    setIndicator("TAB 3").setContent(R.id.textview3));   

  8. mTabHost.setCurrentTab(0);   

  9. }  

上述内容就是Android中怎么实现选项卡功能,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。


网站题目:Android中怎么实现选项卡功能
文章起源:http://cqwzjz.cn/article/jjcsio.html