1,布局

  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:id="@+id/frgmt_main"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:background="#FFFFFF" >
  7.  
  8. <TabHost
  9. android:id="@android:id/tabhost"
  10. android:layout_width="match_parent"
  11. android:layout_height="match_parent"
  12. android:clipChildren="true" >
  13.  
  14. <RelativeLayout
  15. android:layout_width="match_parent"
  16. android:layout_height="match_parent"
  17. android:orientation="vertical" >
  18. <FrameLayout
  19. android:id="@android:id/tabcontent"
  20. android:layout_width="match_parent"
  21. android:layout_height="match_parent"
  22. android:layout_above="@android:id/tabs"> <!-- 注意此句,如果没有此句,当tab1,tab2...过高时,会TabWidget会盖住 -->
  23. <!-- tab1,用include可以引用别处的layout -->
  24. <include
  25. android:id="@+id/tab_weixin"
  26. android:layout_width="match_parent"
  27. android:layout_height="match_parent"
  28. layout="@layout/tab_weixin_layout" />
  29. <!-- tab2,用include可以引用别处的layout -->
  30. <include
  31. android:id="@+id/tab_contacts"
  32. android:layout_width="wrap_content"
  33. android:layout_height="match_parent"
  34. layout="@layout/tab_contacts_layout" />
  35. <!-- tab3,用include可以引用别处的layout -->
  36. <include
  37. android:id="@+id/tab_discovery"
  38. android:layout_width="wrap_content"
  39. android:layout_height="wrap_content"
  40. layout="@layout/tab_discovery_layout" />
  41. <!-- tab4,用include可以引用别处的layout -->
  42. <include
  43. android:id="@+id/tab_me"
  44. android:layout_width="wrap_content"
  45. android:layout_height="wrap_content"
  46. layout="@layout/tab_me_layout" />
  47. </FrameLayout>
  48. <TabWidget
  49. android:id="@android:id/tabs"
  50. android:layout_width="match_parent"
  51. android:layout_height="wrap_content"
  52. android:background="#E0E0E0"
  53. android:layout_alignParentBottom="true"
  54. android:showDividers="none" >
  55. </TabWidget>
  56. </RelativeLayout>
  57. </TabHost>
  58. </FrameLayout>

2,在代码中初始化tab栏

  1. void initTabHostTabs(LayoutInflater inflater){
  2.  
  3. tabHost.setup();
  4.  
  5. ScaleDrawable scale ;
  6. TabSpec tab = tabHost.newTabSpec("weixin");
  7. View tabView1 = inflater.inflate(R.layout.tab_indicator, null, false);
  8. TextView tabTitle = (TextView) tabView1.findViewById(R.id.tab_title);
  9. tabTitle.setText(R.string.tab_weixin);
  10. ImageView tabIcon = (ImageView) tabView1.findViewById(R.id.tab_icon);
  11. tabIcon.setImageResource(R.drawable.tab_weixin_scale);
  12. scale = (ScaleDrawable) tabIcon.getDrawable();
  13. scale.setLevel();
  14.  
  15. tab.setIndicator(tabView1);
  16. tab.setContent(R.id.tab_weixin);
  17. tabHost.addTab(tab);
  18.  
  19. tab = tabHost.newTabSpec("contacts");
  20. View tabView2 = inflater.inflate(R.layout.tab_indicator, null, false);
  21. tabTitle = (TextView) tabView2.findViewById(R.id.tab_title);
  22. tabTitle.setText(R.string.tab_contacts);
  23. tabIcon = (ImageView) tabView2.findViewById(R.id.tab_icon);
  24. tabIcon.setImageResource(R.drawable.tab_contacts_scale);
  25. scale = (ScaleDrawable) tabIcon.getDrawable();
  26. scale.setLevel();
  27.  
  28. tab.setIndicator(tabView2);
  29. tab.setContent(R.id.tab_contacts);
  30. tabHost.addTab(tab);
  31.  
  32. tab = tabHost.newTabSpec("discovery");
  33. View tabView3 = inflater.inflate(R.layout.tab_indicator, null, false);
  34. tabTitle = (TextView) tabView3.findViewById(R.id.tab_title);
  35. tabTitle.setText(R.string.tab_discovery);
  36. tabIcon = (ImageView) tabView3.findViewById(R.id.tab_icon);
  37. tabIcon.setImageResource(R.drawable.tab_discovery_scale);
  38. scale = (ScaleDrawable) tabIcon.getDrawable();
  39. scale.setLevel();
  40.  
  41. tab.setIndicator(tabView3);
  42. tab.setContent(R.id.tab_discovery);
  43. tabHost.addTab(tab);
  44.  
  45. tab = tabHost.newTabSpec("me");
  46. View tabView4 = inflater.inflate(R.layout.tab_indicator, null, false);
  47. tabTitle = (TextView) tabView4.findViewById(R.id.tab_title);
  48. tabTitle.setText(R.string.tab_me);
  49. tabIcon = (ImageView) tabView4.findViewById(R.id.tab_icon);
  50. tabIcon.setImageResource(R.drawable.tab_me_scale);
  51. scale = (ScaleDrawable) tabIcon.getDrawable();
  52. scale.setLevel();
  53.  
  54. tab.setIndicator(tabView4);
  55. tab.setContent(R.id.tab_me);
  56. tabHost.addTab(tab);
  57.  
  58. /* 测试可以向右划动tab
  59. for (int i = 0; i < 5; i++) {
  60. tab = tabHost.newTabSpec("more" + i);
  61. tab.setIndicator("more" + i);
  62. tab.setContent(R.id.tab_me);
  63. tabHost.addTab(tab);
  64. }
  65. */
  66. }

Android 常用UI控件之TabHost(2)简单示例的更多相关文章

  1. Android 常用UI控件之TabHost(5)Tab栏在底部且在最上层也不盖tab页

    tab栏在底部 <TabHost android:id="@android:id/tabhost" android:layout_width="match_pare ...

  2. Android 常用UI控件之TabHost(1)TabHost的两种布局方式

    TabHost是Android中的tab组件. TabHost布局文件的基本结构 TabHost下有个layout,这个layout中有TabWidget与FrameLayout.TabWidget是 ...

  3. Android 常用UI控件之TabHost(4)实现当Tab栏有多个tab时,可以左右滑动

    <!-- <HorizontalScrollView android:id="@+id/horizontalScrollView1" android:layout_wi ...

  4. Android 常用UI控件之TabHost(3)在4.0不显示图标的解决方案

    1,自定义 TabWidget 上每个tab的view 2,用多个图片

  5. Android 常用UI控件之Tab控件的实现方案

    实现Tab的方式有多种 1,ActionBar有两种模式可以实现,但是已经过期 tab模式tab在顶部,分裂模式tab在底部(同时所有action item都在底部). 2,PagerTitleStr ...

  6. 【风马一族_Android】第4章Android常用基本控件

    第4章Android常用基本控件 控件是Android用户界面中的一个个组成元素,在介绍它们之前,读者必须了解所有控件的父类View(视图),它好比一个盛放控件的容器. 4.1View类概述 对于一个 ...

  7. [置顶] Android常用适配器控件

    Android常用适配器控件 列表控件用于显示数据集合,Android不是使用一种类型的控件管理显示和数据,而是将这两项功能分布用列表控件和适配器来实现.列表控件扩展了android.widget.A ...

  8. [Android] Android 让UI控件固定于底部的几种方法

    Android 让UI控件固定于底部的几种方法1.采用linearlayout布局:android:layout_height="0dp" <!-- 这里不能设置fill_p ...

  9. widget 常用UI控件介绍

        一.单选框 单选框实例程序: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&q ...

随机推荐

  1. iBeacon 开发笔记

    iBeacon开发笔记 2015.10.19 airlocate ========= airlocate显示如何使用这个监控范围clbeaconregions. 代码还提供了一个例子,你如何能校准和配 ...

  2. Java线程练习

    /*线程练习创建两个线程,与主线程交替运行 */ class Text extends Thread{    private String name;    Text(String name)     ...

  3. 标准C++中string类的用法

    转自博客园:http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html 相信使用过MFC编程的朋友对CString这个类的印象应该非 ...

  4. 排序算法FIVE:插入排序InsertSort

    /** *插入排序思路:O(n^2) * 最外层一个循环,从第二个数到最后一个,变量为i * 每个数存储在key变量中 * 变量j,是左边已经排好序的数组的上限 * 判断key与前面每一个数比较 1, ...

  5. Apache虚拟主机设置

    Apache支持两种虚拟主机,一种是基于IP地址的,一种是基于域名的. 基于IP地址的虚拟机现在使用的很少,它需要一台服务器需要拥有多个IP地址.基于域名的虚拟主机要求服务器有一个IP地址就可以了,只 ...

  6. SVN菜单说明

    01.SVN Checkout(SVN取出) 点击SVN Checkout,弹出检出提示框,在URL of repository输入框中输入服务器仓库地址,在Checkout directory输入框 ...

  7. 寻找序列中最小的第N个元素(partition函数实现)

    Partition为分割算法,用于将一个序列a[n]分为三部分:a[n]中大于某一元素x的部分,等于x的部分和小于x的部分. Partition程序如下: long Partition (long a ...

  8. hdu 4542 小明系列故事——未知剩余系

    小明系列故事——未知剩余系 题意:操作0表示某数有n个约数,操作1为某数有n个非约数:n <= 47777,若是存在小于2^62的数符合,则输出该数,否则若是不存在输出Illegal,若是大于2 ...

  9. hdu 1251 统计难题 trie入门

    统计难题 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本 ...

  10. Swagger+AutoRest

    Swagger+AutoRest 生成web api客户端(.Net)   简介 对于.net来说,用web api来构建服务是一个不错的选择,都是http请求,调用简单,但是如果真的要在程序中调用, ...