Android之TabHost布局(转)
1.概念
盛放Tab的容器就是TabHost。TabHost的实现有两种方式:
第一种继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost。各个Tab中的内容在布局文件中定义就行了。
第二种方式,不继承TabActivity,在布局文件中定义TabHost即可,但是TabWidget的id必须是@android:id/tabs,FrameLayout的id必须是@android:id/tabcontent。
2.案例
1)继承TabActivity
res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- 定义TabHost组件 -->
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent">
<!-- 定义第一个标签页的内容 -->
<LinearLayout android:id="@+id/tab01" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<!-- 定义两个TextView用于显示标签页中的内容 -->
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="孙悟空-2011/07/12"/>
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="猪八戒-2011/07/10"/>
</LinearLayout>
<!-- 定义第二个标签页的内容 -->
<LinearLayout android:id="@+id/tab02" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="萨僧-2011/07/11"/>
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="唐僧-2011/07/10"/>
</LinearLayout>
<!-- 定义第三个标签页的内容 -->
<LinearLayout android:id="@+id/tab03" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="孙悟空-2011/07/12"/>
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="萨僧-2011/07/08"/>
</LinearLayout>
</TabHost>

HelloTabHost.java

 public class HelloTabHost extends TabActivity {
   @Override
   public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     //调用TabActivity的getTabHost()方法获取TabHost对象
     TabHost tabHost = getTabHost();
     //设置使用TabHost布局
     LayoutInflater.from(this).inflate(R.layout.main, tabHost.getTabContentView(),true);
     //添加第一个标签页
     tabHost.addTab(tabHost.newTabSpec("tab01").setIndicator("已接电话").setContent(R.id.tab01));
     //添加第二个标签页,并在其标签上添加一个图片
     tabHost.addTab(tabHost.newTabSpec("tab02").setIndicator("未接电话",getResources().getDrawable(R.drawable.icon)).setContent(R.id.tab02));
     //添加第三个标签页
     tabHost.addTab(tabHost.newTabSpec("tab03").setIndicator("已拨电话").setContent(R.id.tab03));
   }
 }

运行效果:


2)不继承TabActivity
继承普通Activity,<TabWidget>标签id必须为tabs、<FrameLayout>标签id必须为tabcontent.这个方式在通过findViewById获得TabHost之后,必须要调用setup方法。
main.xml代码

<?xml version="1.0" encoding="utf-8"?>
<!-- TabHost必须包含一个 TabWidget和一个FrameLayout-->
<TabHost android:id="@+id/tabhost" android:layout_width="fill_parent" android:layout_height="wrap_content">
<LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<!-- TabWidget的id属性必须为 @android:id/tabs-->
<TabWidget android:id="@android:id/tabs" android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<!-- FrameLayout的id属性必须为 @android:id/tabcontent-->
<FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:id="@+id/view1" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<TextView android:id="@+id/view2" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<TextView android:id="@+id/view3" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>

Java代码

public class TabHostTest extends Activity {  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        // 获取TabHost对象  
        TabHost tabHost = (TabHost) findViewById(R.id.tabhost);  
        // 如果没有继承TabActivity时,通过该种方法加载启动tabHost  
        tabHost.setup();  
        tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("第一个标签",  
                getResources().getDrawable(R.drawable.icon)).setContent(  
                R.id.view1));  
        tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("第三个标签")  
                .setContent(R.id.view3));  
        tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("第二个标签")  
                .setContent(R.id.view2));  
    }  
} 

效果图

转自:链接
Android之TabHost布局(转)的更多相关文章
- Android底部TabHost API
		今天在项目中遇到了底部TabHost,顺便就写了一个底部TabHost的api继承即可使用非常简单,以下为源代码: 首先是自定义的TabHostActivity,如果要使用该TabHost继承该类即可 ... 
- 12.Android之Tabhost组件学习
		TabHost是整个Tab的容器,TabHost的实现有两种方式: 第一种继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost.各个Tab中的内容在布 ... 
- Android中的布局优化方法
		http://blog.csdn.net/rwecho/article/details/8951009 Android开发中的布局很重要吗?那是当然.一切的显示样式都是由这个布局决定的,你说能不重要吗 ... 
- Android:TabHost实现Tab切换
		TabHost是整个Tab的容器,包含TabWidget和FrameLayout两个部分,TabWidget是每个Tab的表情,FrameLayout是Tab内容. 实现方式有两种: 1.继承TabA ... 
- Android选项卡TabHost方式实现
		1.布局XML: <?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android= ... 
- android学习--TabHost选项卡组件
		TabHost是一种非常有用的组件,TabHost能够非常方便地在窗体上放置多个标签页,每一个标签页获得了一个与外部容器同样大小的组件摆放区域.在手机系统的应用类似"未接电话".& ... 
- Android学习Tabhost、gallery、listview、imageswitcher
		Tabhost控件又称分页控件,在很多的开发语言中都存在.它可以拥有多个标签页,每个标签页可以拥有不同的内容.android中,一个标签页可以放 一个view或者一个activity.TabHost是 ... 
- Android项目--tabhost
		所有牵扯到自定义布局的layout中尽量用RelativeLayout 在通讯录中如果像小米手机的UI那就是viewpager,在这里,我们做成静态的.通过tabhost来做. 1.布局 a) 直接用 ... 
- android 选项卡TabHost
		选项卡主要有TabHost.TabWiget和 FramentLayout3个组件组成,用于实现一个多标签的用户界面,通过他可以将一个复杂的对话分隔成若干个标签页,实现对信息的分类显示和管理.使用给组 ... 
随机推荐
- Majority Number I & || && |||
			Majority Number Given an array of integers, the majority number is the number that occurs more than ... 
- C#导入Exel
			; try { ]; string[] NoExPrentFile = new string[] { "xls", "xlsx" }; ] || fileTyp ... 
- [ 转] [Android]多式样ProgressBar
			多式样ProgressBar 普通圆形ProgressBar 该类型进度条也就是一个表示运转的过程,例如发送短信,连接网络等等,表示一个过程正在执行中. 一般只要在XML布局中定义就可以了. < ... 
- iOS基础框架的搭建/国际化操作
			1.基础框架的搭建 1.1 pod引入常用的第三方类库 1.2 创建基础文件夹结构/目录结构 Resource———存放声音/图片/xib/storyboard 等资源文件 Define——宏定义, ... 
- ubuntu10.04+win7双系统,重装win7后,恢复grub引导菜单以及命令行引导linux
			我在我的小Y上安装了ubuntu10.04和win7旗舰版的双系统,采用的是grub引导.今天win7不知道哪儿出了问题,windows update更新一直报错,(当然360也是打不上滴)网上查了很 ... 
- 【HTTP】WireShark中获取Content-Encoding: gzip时的响应内容
			GZIP格式 详见:http://blog.csdn.net/jison_r_wang/article/details/52068607 表述的很清楚 关键:GZIP头以0x1F8B开始 用WireS ... 
- iOS 本人必装插件
			本人觉得比较好用也实用的Xcode插件记录: 1. Alcatraz 插件通过它来管理 : https://github.com/alcatraz/Alcatraz.git 2. Cocoa ... 
- October 6th 2016 Week 41st Thursday
			The outer world you see is a reflection of your inner self. 你拥有什么样的内心,你就会看到什么样的世界. And we eventually ... 
- 模拟赛1102d2
			/* φ(n)=φ(p^k)=p^k-p^(k-1)=(p-1)*p^(k-1) φ(m*n)=φ(m)*φ(n) 直接套公式做,因为分解质因数时,只分解一个数,所以可以不打素数表,只将n分解到√n就 ... 
- 使用vsphere client 克隆虚拟机
			免费的VMWare ESXi5.0非常强大,于是在vSphere5.0平台中ESXi取代了ESX.,使用ESXi经常会遇到这样的问题,我需要建立多个虚拟机,都是windows2003操作系统,难道必须 ... 
