本文主要参考自:http://blog.csdn.net/wulianghuan/article/details/8588947 (里面有TabHost第二种定义的方式,继承TabActivity)

TabHost就是一个选项卡,类似于tab。这里我定义了三个标签,点击后会切换出不同的内容。之前用Fragment实现过类似的效果,总觉得还是Fragment+tab好用点。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <TabHost
android:id="@+id/tablehost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" > <LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" /> <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" > </FrameLayout>
</LinearLayout>
</TabHost> </LinearLayout>

每个选项卡点击后的内容的布局。其实就是在上面定义的FrameLayout中的内容

home.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="fill_parent"
android:background="#0000FF"
android:id="@+id/home"
android:orientation="vertical"> <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="home"
/> </LinearLayout>

comment.xml / save.xml布局和home一样。

下面是java代码——MainActivity.java

package com.example.tabhost;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //取得TabHost
TabHost tabhost = (TabHost) findViewById(R.id.tablehost);
tabhost.setup();
LayoutInflater inflater = LayoutInflater.from(this);
//把配置文件转换为显示TabHost内容的FrameLayout中的层级
inflater.inflate(R.layout.home, tabhost.getTabContentView());
inflater.inflate(R.layout.comment, tabhost.getTabContentView());
inflater.inflate(R.layout.save, tabhost.getTabContentView());
//设置HOME标签
TabSpec spec1 = tabhost.newTabSpec("HOME").
setIndicator("HOME"); // setIndicator设置标题
//设置HOME模块显示内容
spec1.setContent(R.id.home);
tabhost.addTab(spec1);
TabSpec spec2 = tabhost.newTabSpec("COMMENT").setIndicator("COMMENT");
spec2.setContent(R.id.comment);
tabhost.addTab(spec2); TabSpec spec3 = tabhost.newTabSpec("SAVE").setIndicator("SAVE");
spec3.setContent(R.id.save);
tabhost.addTab(spec3); }
}

TabHost的初步使用的更多相关文章

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

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

  2. Robotium原理初步--Android自动化测试学习历程

    章节:自动化基础篇——Robotium原理初步(第四讲) 主要讲解内容与笔记: 一.基于控件 1.spinner——下拉菜单 2.TabHost——左右滑动选择菜单,类似电话本 3.Gallery—— ...

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

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

  4. 移动端之Android开发的几种方式的初步体验

    目前越来越多的移动端混合开发方式,下面列举的大多数我都略微的尝试过,就初步的认识写个简单的心得: 开发方式 开发环境 是否需要AndroidSDK 支持跨平台 开发语言&技能 MUI Win+ ...

  5. android 使用Tabhost 发生could not create tab content because could not find view with id 错误

    使用Tabhost的时候经常报:could not create tab content because could not find view with id 错误. 总结一下发生错误的原因,一般的 ...

  6. CSharpGL(29)初步封装Texture和Framebuffer

    +BIT祝威+悄悄在此留下版了个权的信息说: CSharpGL(29)初步封装Texture和Framebuffer +BIT祝威+悄悄在此留下版了个权的信息说: Texture和Framebuffe ...

  7. Android自定义View初步

    经过上一篇的介绍,大家对于自定义View一定有了一定的认识,接下来我们就以实现一个图片下显示文字的自定义View来练习一下.废话不多说,下面进入我们的正题,首先看一下我们的思路,1.我们需要通过在va ...

  8. 初步认识Node 之Node为何物

    很多人即便是在使用了Node之后也不知道它到底是什么,阅读完本文你应该会有一个初步的.具体的概念了.    Node的目标 提供一种简单的构建可伸缩网络程序的方法.那么,什么是可伸缩网络程序呢?可伸缩 ...

  9. [入门级] 基于 visual studio 2010 mvc4 的图书管理系统开发初步 (二)

    [入门级] 基于 visual studio 2010 mvc4 的图书管理系统开发初步 (二) Date  周六 10 一月 2015 By 钟谢伟 Category website develop ...

随机推荐

  1. WinForm1

    一.窗体的各种属性 二.控件 1.公共控件 2.容器控件 3.菜单控件

  2. docker commit 制作镜像

    docker commit -m="commit jdk" --author="gutianlangyu" ae56f6cad215 gutianlangyu/ ...

  3. 工作流调度器azkaban2.5.0的安装和使用

    为什么需要工作流调度系统 一个完整的数据分析系统通常都是由大量任务单元组成: shell脚本程序,java程序,mapreduce程序.hive脚本等 各任务单元之间存在时间先后及前后依赖关系 为了很 ...

  4. io编程,bio,nio,aio

    本文会从传统的BIO到NIO再到AIO自浅至深介绍,并附上完整的代码讲解. 下面代码中会使用这样一个例子:客户端发送一段算式的字符串到服务器,服务器计算后返回结果到客户端. 代码的所有说明,都直接作为 ...

  5. Java 发送http post 请求

    package com.sm.utils; import java.io.BufferedReader; import java.io.InputStreamReader; import java.i ...

  6. ajax jqplot ssh实现图表的持续更新

    实现功能: 数据库有新数据图表会更新到 数据库查询使用ssh框架中的hibernate 想法: 画图表的ajaxautoruncopy.jsp利用ajax收到7-13.jsp传过来的数据 7-13.j ...

  7. Java多线程runnable

    主要为大家分享Java多线程怎么实现Runnable方式 一 :主要步骤 1.定义实现Runnable接口 2.覆盖Runnable接口中run方法,将线程要运行的代码存在run方法里 3.用Thre ...

  8. 【斜优DP】bzoj4518-Sdoi2016征途

    一.斜率优化DP与决策单调性 这里浅显(并且不严谨)地说明一下标题中的两个名词: 斜率优化DP:状态转移方程形如f[i]=min/max{f[k]+(x[i]-x[k])^y}的一类DP问题: 决策单 ...

  9. DrawFrameControl 绘制标准控件

    BOOL DrawFrameControl( HDC hDC,        // 设备环境句柄    LPRECT lpRect,  // 矩形区域    UINT nType,     // 控件 ...

  10. 读think in java有感

    .... 2.2.1 保存到什么地方 程序运行时,我们最好对数据保存到什么地方做到心中有数.特别要注意的是内存的分配.有六个地方都可以保存数据: (1) 寄存器.这是最快的保存区域,因为它位于和其他所 ...