控件TabHost的功能和web技术中的“选项卡”控件显示的效果一样。都是用最小的空间显示更多的数据。下面直接通过例子看Tabhost的用法。

示例1:通过继承TabActivity实现

布局文件:

<!-- 布局必须为FrameLayout -->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"  ><!-- 定义一个LinearLayout,用于显示第个Tab页的内容,每个LinearLayout代表一页的内容 --><LinearLayoutandroid:id="@+id/tab1"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="第一页"/>    </LinearLayout><!-- 定义第二个个LinearLayout,用于显示第个Tab页的内容 --> <LinearLayoutandroid:id="@+id/tab2"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"> <TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="第二页"/>    </LinearLayout>
</FrameLayout>

MainActivity.java内容:

public class MainActivity extends TabActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//获取TabHost的引用TabHost tabHost = getTabHost();//获取视图,并LayoutInflater.from(this).inflate(R.layout.activity_main,tabHost.getTabContentView(), true);//添加Tab也的内容。tabHost.addTab(tabHost.newTabSpec("tab1")//设置Tab页的标示.setIndicator("导航一",//设置标题getResources().getDrawable(R.drawable.ic_launcher))//设置标题图片,也可以不写.setContent(R.id.tab1));//设置Tab内容tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("导航二",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.tab2));}
}

运行结果如图:

示例2:

MainActivity.java:

public class MainActivity extends TabActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//获取TabHost的引用TabHost tabHost = getTabHost();//为Tab添加内容tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("导航一",getResources().getDrawable(R.drawable.ic_launcher)).setContent(new Intent(this, Act2.class)));//与示例1不同。这里直接添加Intent的引用tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("导航二",getResources().getDrawable(R.drawable.ic_launcher)).setContent(new Intent(this, Act3.class)));}
}

注:这个方法要另建两个Activity及相应的xml文件。每一个Activity既为每个Tab页的内容。在此代码就不在给出。运行结果和示例1完全相同。

示例3:自定义TabHost

main.xml:

<!-- 直接引用系统提供的Tabhost组件即可 -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"xmlns:android1="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" ><TabHostandroid1:id="@+id/tabhost"android1:layout_width="match_parent"android1:layout_height="match_parent"android1:layout_alignParentLeft="true"android1:layout_alignParentTop="true" ><LinearLayoutandroid1:layout_width="match_parent"android1:layout_height="match_parent"android1:orientation="vertical" ><TabWidgetandroid1:id="@android:id/tabs"android1:layout_width="match_parent"android1:layout_height="80dp" ></TabWidget><FrameLayoutandroid1:id="@android:id/tabcontent"android1:layout_width="match_parent"android1:layout_height="match_parent" ><LinearLayoutandroid1:id="@+id/tab1"android1:layout_width="match_parent"android1:layout_height="match_parent"android1:orientation="vertical" ><TextViewandroid1:layout_width="wrap_content"android1:layout_height="wrap_content"android1:text="第一页" />                  </LinearLayout><LinearLayoutandroid1:id="@+id/tab2"android1:orientation="vertical"android1:layout_width="match_parent"android1:layout_height="match_parent" ><TextViewandroid1:layout_width="wrap_content"android1:layout_height="wrap_content"android1:text="第二页" /></LinearLayout><LinearLayoutandroid1:id="@+id/tab3"android1:orientation="vertical"android1:layout_width="match_parent"android1:layout_height="match_parent" ><TextViewandroid1:layout_width="wrap_content"android1:layout_height="wrap_content"android1:text="第三页" /></LinearLayout></FrameLayout></LinearLayout></TabHost>
</RelativeLayout>

另外还要定义一个cell.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><ImageViewandroid:id="@+id/p_w_picpathView1"android:layout_width="wrap_content"android:layout_height="wrap_content"/><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="TextView" />
</LinearLayout>

接下来是MainActivity:

public class MainActivity extends Activity {private TabWidget tabWidget;private TabHost tabHost;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//获取Tabhost控件tabHost=(TabHost)findViewById(R.id.tabhost);//初始化tabHost.setup();//tabWidget=tabHost.getTabWidget();//创建tab页内容TabSpec spec1=tabHost.newTabSpec("tab1");//通过自定义的函数创建标题。spec1.setIndicator(CreatTabView(tabHost,"导航1",R.drawable.ic_launcher));spec1.setContent(R.id.tab1);tabHost.addTab(spec1);TabSpec spec2=tabHost.newTabSpec("tab2");spec2.setIndicator(CreatTabView(tabHost,"导航2",R.drawable.ic_launcher));spec2.setContent(R.id.tab2);tabHost.addTab(spec2);TabSpec spec3=tabHost.newTabSpec("tab3");spec3.setIndicator(CreatTabView(tabHost,"导航3",R.drawable.ic_launcher));spec3.setContent(R.id.tab3);tabHost.addTab(spec3);/*showSpec();tabHost.setOnTabChangedListener(new OnTabChangeListener() {@Overridepublic void onTabChanged(String tabId) {showSpec();}          });     */     }/*public void showSpec() {for (int i = 0; i < tabWidget.getChildCount(); i++) {// 获得当前的视图对象View v = tabWidget.getChildAt(i);// 判断当前点击的视图对象是否与得到的视图对象相同if (tabHost.getCurrentTab() == i) {v.setBackgroundResource(R.drawable.ic_blue);} else {v.setBackgroundResource(R.drawable.ic_green);}}}*//*** 自定义tab显示样式(图片加文字)*/public View CreatTabView(TabHost tabHost,String tabtitle,int tabID){//获取视图的引用View tabview=LayoutInflater.from(this).inflate(R.layout.cell, null);//设置组件内容TextView tv=(TextView)tabview.findViewById(R.id.textView1);tv.setText(tabtitle);ImageView iv=(ImageView)tabview.findViewById(R.id.p_w_picpathView1);iv.setBackgroundResource(tabID);//返回一个视图       return tabview;}
}

运行结果如下图所示,点击效果不明显。明显不是我所需要的结果。将注释部分放开后,显示效果如右图,这时需要我们添加点击效果。

转载于:https://blog.51cto.com/shaotao/1283034

Android -- TabHost相关推荐

  1. Android Tabhost with FragmentActivity

    此文解决我这两天的问题,故转载:原文Android Tabhost with FragmentActivity   2012-05-07 更新)接續Android TabHost中切換Activity ...

  2. Android TabHost中切換、修改需要显示的Activity

    上個星期跟盛哥試了一段時間使用Fragment後還是不得其門而入(兩個人對Fragment都還不太熟悉),卡住的原因是現在有兩個Tab,當Tab1進入到第二個畫面,Tab2進到第三個畫面,但使用者切回 ...

  3. Android TabHost和xml定义Menu应用

    Android TabHost和xml定义Menu应用 http://files.cnblogs.com/hnrainll/TabMenu.zip

  4. android Tabhost部件

    本文结合源代码和实例来说明TabHost的用法. 使用TabHost 可以在一个屏幕间进行不同版面的切换,例如android自带的拨号应用,截图: 查看tabhost的源代码,主要实例变量有: pri ...

  5. android tabhost --android UI 学习

    2019独角兽企业重金招聘Python工程师标准>>> 实现TabHost有三种方式:继承自TabActivity,ActivityGroup和自定义的Activity 实现效果图: ...

  6. 【Android 应用开发】Android - TabHost 选项卡功能用法详解

    TabHost效果图 : 源码下载地址 : http://download.csdn.net/detail/han1202012/6845105        . 作者 :万境绝尘  转载请注明出处  ...

  7. Android --- TabHost 切换时,改变选项卡下字体的状态(大小、加粗、默认被选中第一个)

    上效果图: MiddleFragment.java 代码如下 import android.os.Bundle; import android.view.LayoutInflater; import ...

  8. android tabhost 多个activity,Android:TabHost中Activity的生命周期问题

    用过TabHost制作多个activity的分页效果的朋友应该知道,tabhost中镶嵌的activity的onCreate和onDestroy是和tabhost关联的,在创建了tabhost之后,第 ...

  9. Android TabHost中实现标签的滚动以及一些TabHost开发的奇怪问题

    最近在使用TabHost的时候遇到了一些奇怪的问题,在这里总结分享备忘一下. 首先说一点TabActivity将会被FragmentActivity所替代,但是本文中却是使用的TabActivity. ...

  10. Android TabHost中Activity之间传递数据

    例子1: TabHost tabhost = (TabHost) findViewById(android.R.id.tabhost);tabhost.setup(this.getLocalActiv ...

最新文章

  1. Using Apache2 with JBoss AS7 on Ubuntu
  2. mac mysql本地连接数_如何用command line链接mysql
  3. C++之智能指针std::shared_ptr简单使用和理解
  4. vba执行linux命令,如何使用vba的shell()运行参数的.exe?
  5. Eclipse+Pydev环境搭建
  6. JavaScript之子类构建工具
  7. Oracle用户及角色的权限管理[Oracle基础]
  8. 厦门大学计算机软件学院,厦门大学软件学院研究生招生信息_厦门大学软件学院2019-研究生招生报名查询系统...
  9. Android PackageInstaller:安装应用的应用,从三流Android外包到秒杀阿里P7
  10. Delphi时间戳Unix转换DateTimeToUnix和UnixToDateTime
  11. HTML小游戏2—— 2048网页版(附完整源码)
  12. Cisco Packet Tracer 思科交换机模拟器常见命令
  13. 这些屏幕特效是咋实现的
  14. 从 DOM 中移除一个元素
  15. 一站式解决网站死链问题
  16. python数据结构与算法 20 递归和递归三定律
  17. 计算机开机按f1,开机要按F1怎么解决?电脑开机不按F1进去系统的方法
  18. 西部数据移动硬盘设置密码教程
  19. MATLAB | 经典力学框架下的三体运动数值模拟软件
  20. 七牛云配置怎么配CDN

热门文章

  1. 开发内功修炼CPU篇
  2. DPDK学习0 -- 学习步骤
  3. Android网卡(Wifi/Lan)带宽测试
  4. linux调度器(二)——CFS模型
  5. scala中的作用域保护
  6. hive join 数据倾斜 真实案例
  7. python random 生成随机数
  8. 计算机体系结构----指令流水线吞吐率、效率计算
  9. kancloud mysql内核_锁 · Mysql · 看云
  10. 数值范围_涉及数值范围的答复及撰写建议