android 之 TabHost
TabHost的实现有两种方式,第一种继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost。各个Tab中的内容在布局文件中定义就行了。
mainActivity.xml
private TabHost myTabHost;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
myTabHost = this.getTabHost();
LayoutInflater.from(this).inflate(R.layout.main,
myTabHost.getTabContentView(), true);
myTabHost.addTab(myTabHost
.newTabSpec("选项卡1")
.setIndicator("选项卡1",
getResources().getDrawable(R.drawable.img01))
.setContent(R.id.ll01));
myTabHost.addTab(myTabHost
.newTabSpec("选项卡2")
.setIndicator("选项卡2",
getResources().getDrawable(R.drawable.img02))
.setContent(R.id.ll01));
myTabHost.addTab(myTabHost
.newTabSpec("选项卡3")
.setIndicator("选项卡3",
getResources().getDrawable(R.drawable.img03))
.setContent(R.id.ll03));}
Tab内容布局文件:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:id="@+id/ll01" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="center_horizontal"
android:orientation="vertical">
<EditText android:id="@+id/widget34" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="EditText"
android:textSize="18sp">
</EditText>
<Button android:id="@+id/widget30" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Button">
</Button></LinearLayout>
<LinearLayout android:id="@+id/ll02" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="center_horizontal"
android:orientation="vertical">
<AnalogClock android:id="@+id/widget36"
android:layout_width="wrap_content" android:layout_height="wrap_content">
</AnalogClock>
</LinearLayout>
<LinearLayout android:id="@+id/ll03" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="center_horizontal"
android:orientation="vertical">
<RadioGroup android:id="@+id/widget43"
android:layout_width="166px" android:layout_height="98px"
android:orientation="vertical">
<RadioButton android:id="@+id/widget44"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="RadioButton">
</RadioButton>
<RadioButton android:id="@+id/widget45"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="RadioButton">
</RadioButton>
</RadioGroup></LinearLayout>
</FrameLayout>

第二种方式,不继承TabActivity,在布局文件中定义TabHost即可,但是TabWidget的id必须是@android:id/tabs,FrameLayout的id必须是@android:id/tabcontent。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/hometabs"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<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 android:id="@android:id/tabs"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TabWidget>
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:id="@+id/view1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:text="Tab1"/>
<TextView android:id="@+id/view2"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:text="Tab2"/>
<TextView android:id="@+id/view3"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:text="Tab3"/>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
mainActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);TabHost tabHost = (TabHost) findViewById(R.id.tabhost);
tabHost.setup();
TabWidget tabWidget = tabHost.getTabWidget();tabHost.addTab(tabHost
.newTabSpec("tab1")
.setIndicator("tab1",
getResources().getDrawable(R.drawable.img01))
.setContent(R.id.view1));tabHost.addTab(tabHost
.newTabSpec("tab2")
.setIndicator("tab2",
getResources().getDrawable(R.drawable.img02))
.setContent(R.id.view2));tabHost.addTab(tabHost
.newTabSpec("tab3")
.setIndicator("tab3",
getResources().getDrawable(R.drawable.img03))
.setContent(R.id.view3));
android 之 TabHost的更多相关文章
- Android底部TabHost API
今天在项目中遇到了底部TabHost,顺便就写了一个底部TabHost的api继承即可使用非常简单,以下为源代码: 首先是自定义的TabHostActivity,如果要使用该TabHost继承该类即可 ...
- 12.Android之Tabhost组件学习
TabHost是整个Tab的容器,TabHost的实现有两种方式: 第一种继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost.各个Tab中的内容在布 ...
- android之TabHost(下)
首先建立res/layout/tab.xml文件 编写代码如下: <?xml version="1.0" encoding="utf-8"?> &l ...
- android之TabHost(上)
首先建立文件res/layout/tab.xml 代码如下: <?xml version="1.0" encoding="utf-8"?> < ...
- 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实现导航
参考 http://blog.csdn.net/xixinyan/article/details/6771341 http://blog.sina.com.cn/s/blog_6b04c8eb0101 ...
- android的tabhost+RadioGroup+PopupWindow
根据网上的代码稍作修改了下,放着记录学习. 效果图如下: 主代码如下: package com.andyidea.tabdemo; import android.app.TabActivity; im ...
- android学习--TabHost选项卡组件
TabHost是一种非常有用的组件,TabHost能够非常方便地在窗体上放置多个标签页,每一个标签页获得了一个与外部容器同样大小的组件摆放区域.在手机系统的应用类似"未接电话".& ...
- Android学习Tabhost、gallery、listview、imageswitcher
Tabhost控件又称分页控件,在很多的开发语言中都存在.它可以拥有多个标签页,每个标签页可以拥有不同的内容.android中,一个标签页可以放 一个view或者一个activity.TabHost是 ...
随机推荐
- 17995 Stupid thief 组合数学
17995 Stupid thief 时间限制:1000MS 内存限制:65535K提交次数:0 通过次数:0 题型: 编程题 语言: 不限定 Description A stupid thie ...
- vue.js2.0实战填坑记录
访https://github.com/bailicangdu/vue2-elm的PC商城 在创建的 router 对象中,如果不配置 mode,就会使用默认的 hash 模式,该模式下会将路径格式化 ...
- bootstrap-table 基础用法
1.需要添加的引用. <script src="@Url.Content("~/js/jquery-2.1.1.js")"></script& ...
- 【踩坑】Safari不兼容webpack封装的vue项目
刚完成 Iblog 博客项目,在 chrome 浏览器调试完后,用 Safari 打开网站,页面一直停留在加载状态. 后来网上说这是 Safari 不支持 ES6 所致. 经过搜索,在 github ...
- div+css 布局经验 - 最简单的 = 最不变形的(原创技巧)
站酷几年了 一直饱受其恩泽 尤为感激 一直想奉献些什么 但是苦于水平 苦于奔波 今天静下心来 为大家奉献下 自己的div+css 经验 ,以下观点只代表 深海个人立场 希望为初学者提供一条" ...
- Node.js 打造实时多人游戏框架
在 Node.js 如火如荼发展的今天,我们已经可以用它来做各种各样的事情.前段时间UP主参加了极客松活动,在这次活动中我们意在做出一款让“低头族”能够更多交流的游戏,核心功能便是 Lan Party ...
- java冒泡排序和快速排序代码
冒泡排序: package nicetime.com; //基本思想:在要排序的一组数中,对当前还未排好序的范围内的全部数,自上而下对相邻的两个数依次进行比较和调整,// 让较大的数往下沉,较小的往上 ...
- css3 省略号
overflow: hidden; text-overflow: ellipsis; white-space: nowrap; 也无需给元素设置固定宽度!
- Codeforces Gym 100650B Countdown (离线)
题目链接:http://codeforces.com/gym/100650 根据给出的树和d,求出一些结点,这些结点形成子树的第d层结点数应该尽量多,具体要求可以参考题目. dfs一个结点前保存询问深 ...
- java日期操作的工具类时间格式的转换
package cn.itcast.oa.util; import java.text.ParseException; import java.text.SimpleDateFormat;import ...