当前位置:首页 > 编程开发

Android选项卡具体代码编写方式介绍

webgou16年前 (2010-08-08)编程开发234

在对Android操作系统进行相应修改中,我们可以发现,这一系统的编程方式非常简单易懂,方便开发人员实现各种功能需求。在这里就先从Android选项卡的实现来具体了解一下这一系统的编写方式。

首先创建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代码

  1. public class HelloTabWidget extends TabActivity   
  2. 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选项卡的具体实现方式就为大家介绍到这里。

扫描二维码推送至手机访问。

版权声明:本文由知了博客发布,如需转载请注明出处。

本文链接:https://www.webgou.info/?id=232

标签: android界面
分享给朋友:

“Android选项卡具体代码编写方式介绍” 的相关文章

IE最新的极光0day

var sc = unescape(" dddd"); var sss = Array(826, 679, 798, 224, 770, 427, 819, 770, 707, 805, 693, 679, 784, 707, 280, 238, 259, 819, 336, 6…

vi显示行数命令

vi中显示行数命令::set number跳转到指定行:xx …

Android术语列表概览

1,apk扩展名:apk是Android包的扩展名,一个Android包包含了与某个Android应用程序相关的所有文件,apk文件将 AndroidManifest.xml文件、应用程序代码(dex文件)、资源文件和其他文件组成一个压缩包,一个项目只能打包压缩成一个apk文件。2, dex扩展名:…

怎样使用Eclipse来开发Android源码

用eclipse + ADT作为android开发工 具,可以说是很方便的,在HelloActivity小程序里我们就感觉到eclipse功能的强大。那么,我们可以用 eclipse来开发android源码吗?如果我们直接把android源码里一个工程导入eclipse,一般来说都会出现错误,说许…

android2.2 源码获取错误:error.GitError: platform/cts rev-list

pos@ubuntu:~/a2t$ repo syncFetching projects: 100% (158/158), done.  Traceback (most recent call last):  File "/home/pos/a2t/.repo/repo…

线程

线程是由两个部分组成的: * 一个是线程的内核对象,操作系统用它来对线程实施管理。内核对象也是系统用来存放 线程统计信息的地方。 * 另一个是线程堆栈,它用于维护线程在执行代码时需要的所有函数参数和局部变量 进程是不活泼的。进程从来不执行任何东西,它只是线程的…

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。