选项卡主要有TabHost、TabWiget和 FramentLayout3个组件组成,用于实现一个多标签的用户界面,通过他可以将一个复杂的对话分隔成若干个标签页,实现对信息的分类显示和管理。使用给组件不仅可以使界面美观大方,还可以有效地减少窗体个数。

在Android中,实现选项卡的一半步骤如下:

(1)在布局文件中添加实现选项卡所需的TabHost、TabWiget和 FramentLayout组件。

(2)编写各个标签页中要显示内容所对应的XML布局文件。

(3)在Activity个,获取并初始化TabHost组件。

(4)为TabHost对象添加标签页。

1.主布局文件:

activity_main.layout.xml:

  1. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
  3. android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
  4. android:paddingRight="@dimen/activity_horizontal_margin"
  5. android:paddingTop="@dimen/activity_vertical_margin"
  6. android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
  7. android:id="@android:id/tabhost">
  8. <RelativeLayout
  9. android:layout_width="fill_parent"
  10. android:layout_height="fill_parent"
  11. android:orientation="vertical">
  12. <TabWidget
  13. android:layout_width="fill_parent"
  14. android:layout_height="wrap_content"
  15. android:id="@android:id/tabs"
  16. android:layout_alignParentBottom="true"></TabWidget>
  17. <FrameLayout
  18. android:layout_width="fill_parent"
  19. android:layout_height="fill_parent"
  20. android:id="@android:id/tabcontent"></FrameLayout>
  21. </RelativeLayout>
  22. </TabHost>

2.编写各标签页要显示内容对应的XML布局文件。

tab1.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent" android:layout_height="match_parent"
  4. android:orientation="vertical"
  5. android:id="@+id/linearLayout01">
  6. <TextView
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:text="我是Tab1"/>
  10. <TextView
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:background="@drawable/img01"/>
  14. </LinearLayout>

tab2.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent" android:layout_height="match_parent"
  4. android:orientation="vertical"
  5. android:id="@+id/linearLayout02">
  6. <TextView
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:text="我是Tab2"/>
  10. <TextView
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:background="@drawable/img02"/>
  14. </LinearLayout>

MainActivity.java

  1. public class MainActivity extends ActionBarActivity {
  2. private TabHost tabHost=null;
  3. private TabWidget tabWidget=null;
  4. private FrameLayout frameLayout=null;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9. tabHost= (TabHost) findViewById(android.R.id.tabhost); //声明TabHost组件对象
  10. tabHost.setup();  //初始化TabHost组件
  11. //为TabHost对象添加标签页。
  12. LayoutInflater inflater=LayoutInflater.from(this); //申明LayoutInflater对象
  13. inflater.inflate(R.layout.tab1,tabHost.getTabContentView());
  14. inflater.inflate(R.layout.tab2,tabHost.getTabContentView());
  15. tabHost.addTab(tabHost.newTabSpec("未接电话")
  16. .setIndicator("未接电话")
  17. .setContent(R.id.linearLayout01)); //添加第一个标签页
  18. tabHost.addTab(tabHost.newTabSpec("已接电话")
  19. .setIndicator("已接电话",getResources().getDrawable(R.drawable.icon))
  20. .setContent(R.id.linearLayout02)); //添加第二个标签页
  21. }
  22. @Override
  23. public boolean onCreateOptionsMenu(Menu menu) {
  24. // Inflate the menu; this adds items to the action bar if it is present.
  25. getMenuInflater().inflate(R.menu.menu_main, menu);
  26. return true;
  27. }
  28. @Override
  29. public boolean onOptionsItemSelected(MenuItem item) {
  30. // Handle action bar item clicks here. The action bar will
  31. // automatically handle clicks on the Home/Up button, so long
  32. // as you specify a parent activity in AndroidManifest.xml.
  33. int id = item.getItemId();
  34. //noinspection SimplifiableIfStatement
  35. if (id == R.id.action_settings) {
  36. return true;
  37. }
  38. return super.onOptionsItemSelected(item);
  39. }
  40. }

setIndicator()可以设置选项卡得图标和文字。

需要注意几点是: 

                           TabHost必须为 android:id="@android:id/tabhost"

                           TabWidget 必须为 android:id="@android:id/tabs" ,

  FrameLayout android:id="@android:id/tabcontent" ;

效果图(更改TabWiget位置可以实现让TabWiget显示在顶端或低端):

android 选项卡TabHost的更多相关文章

  1. Android选项卡TabHost方式实现

    1.布局XML: <?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android= ...

  2. Android选项卡TabHost功能和用法

    1.布局文件 <TabHost xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...

  3. Android 自学之选项卡TabHost

    选项卡(TabHost)是一种非常实用的组件,TabHost可以很方便地在窗口上放置多个标签页,每个标签页相当于获得了一个与外部容器相同大小的组建摆放区域.通过这种方式,就可以在一个容器中放置更多组件 ...

  4. android学习--TabHost选项卡组件

    TabHost是一种非常有用的组件,TabHost能够非常方便地在窗体上放置多个标签页,每一个标签页获得了一个与外部容器同样大小的组件摆放区域.在手机系统的应用类似"未接电话".& ...

  5. Android零基础入门第63节:过时但仍值得学习的选项卡TabHost

    原文:Android零基础入门第63节:过时但仍值得学习的选项卡TabHost 由于前几天参加一个学习培训活动,几乎每天都要从早晨7点到晚上一两点,没有什么时间来分享,实在抱歉中间断更了几天.从今天开 ...

  6. android自定义tabhost,tabcontent用intent获得

    地址:http://my.oschina.net/aowu/blog/36282 自己改的自定义tabhost组建,效果图如左.有更好的朋友可以相互交流一下,嘿嘿. 1.先上AndroidManife ...

  7. 浅谈Android选项卡(二)

    前面简单介绍了选项卡,下面以及后面的几篇文章介绍下Android选项卡的几种简单实现方法. http://blog.csdn.net/xia215266092/article/details/9613 ...

  8. android中TabHost和RadioGroup

    android底部菜单应用 博客分类: android--UI示例 TabHostMenuRadioGroupButton  在android中实现菜单功能有多种方法. Options Menu:用户 ...

  9. Android选项卡学习

    什么是选项卡 顶部的导航条就是选项卡了. Android开发中添加选项卡的步骤 图片不太懂上代码: activity_main.xml <?xml version="1.0" ...

随机推荐

  1. poj3270 && poj 1026(置换问题)

    | 1 2 3 4 5 6 | | 3 6 5 1 4 2 | 在一个置换下,x1->x2,x2->x3,...,xn->x1, 每一个置换都可以唯一的分解为若干个不交的循环 如上面 ...

  2. bzoj 2734: [HNOI2012]集合选数

    题目描述 <集合论与图论>这门课程有一道作业题,要求同学们求出{1, 2, 3, 4, 5}的所有满足以 下条件的子集:若 x 在该子集中,则 2x 和 3x 不能在该子集中. 同学们不喜 ...

  3. bzoj 1925: [Sdoi2010]地精部落

    Description 传说很久以前,大地上居住着一种神秘的生物:地精. 地精喜欢住在连绵不绝的山脉中.具体地说,一座长度为 N 的山脉 H可分 为从左到右的 N 段,每段有一个独一无二的高度 Hi, ...

  4. 在QEMU中调试ARM程序【转】

    转自:http://linuxeden.com/html/develop/20100820/104409.html 最近我想调试一个运行在QEMU模拟ARM系统中的Linux程序.我碰到过一些麻烦,因 ...

  5. 毕业设计-JSP论文盲审系统

    之前做的一款jsp的论文盲审系统,ssh框架的,学生提交论文,系统管理员将论文分配给教员,教员在不知学员是谁的情况之下,对论文进行打分,然后提交给系统,最后系统发布成绩,供学员查看. 整体做的还不错, ...

  6. Git常用命令及常见问题解决

    $ mkdir xxx       ----创建xxx目录 $ cd learngit     ----切到xxx目录下 $ pwd               ----查看当前文件所在目录 $ gi ...

  7. Unix系统的文件目录项的内容是什么,这样处理的好处是什么?

    (Unix系统采用树型目录结构,而且目录中带有交叉勾链.每个目录表称为一个目录文件.一个目录文件是由目录项组成的.) 每个目录项包含16个字节,一个辅存磁盘块(512B)包含32个目录项.在目录项中, ...

  8. I/O控制的主要功能

    主要功能: 1.  解释用户的I/O系统调用.将用户I/O系统调用转换为I/O控制模块认识的命令模式. 2.  设备驱动.根据得到的I/O命令,启动物理设备完成指定的I/O操作. 3.  中断处理.对 ...

  9. 将jdbc连接明文密码加密方案

    最近没有及时写文章,把最近处理的几个问题集中了一下写出来.这篇文章是关于如何处理spring项目中引入数据库连接等 使用的用户名和密码的明文进行加密.防止被他人窃取利用. 我们选择的加密方式为DES加 ...

  10. 匿名函数lambda

    匿名函数的定义 在python中,匿名函数的定义如下: func =lambda x:x+1 #定义匿名函数,x为传参,x+1为返回值,func为函数名 res = func(10) #执行匿名函数 ...