Android中TabHost中实现标签的滚动以及一些TabHost开发的奇怪问题
最近在使用TabHost的时候遇到了一些奇怪的问题,在这里总结分享备忘一下。
首先说一点TabActivity将会被FragmentActivity所替代,但是本文中却是使用的TabActivity。
下面说说本程序能够实现的功能:
- 实现TabHost中的标题栏能够横向滚动;
- 自定义标题栏的大小和样式;
- 自定义标题栏的分割线的样式;
下面分几步来分别实现以上的功能:
第一步,先实现一个基本的TabHost的展现
详细的说明可以在网上其它地方搜的,主要就是注意一点,控件的id的是固定的不能随便更改,并且@和id之间不能加+;
Activity的代码如下:

1 public class TabhostTestActivity extends TabActivity implements
2 TabContentFactory {
3 private final String[] tabTitle = { "测试Tab标签1", "测试Tab标签2", "测试Tab标签3",
4 "测试Tab标签4", "测试Tab标签5", "测试Tab标签6", "测试Tab标签7", "测试Tab标签8",
5 "测试Tab标签9" };
6
7 /** Called when the activity is first created. */
8 @Override
9 public void onCreate(Bundle savedInstanceState) {
10 super.onCreate(savedInstanceState);
11 setContentView(R.layout.tabhost);
12 TabHost th = getTabHost();
13 for (int i = 0; i < tabTitle.length; i++) {
14 TextView view = (TextView) getLayoutInflater().inflate(
15 R.layout.tabwighet, null);
16 view.setText(tabTitle[i]);
17 th.addTab(th.newTabSpec(tabTitle[i]).setIndicator(view)
18 .setContent(this));
19 }
20 }
21
22 @Override
23 public View createTabContent(String tag) {
24 View view = new View(this);
25 if (tabTitle[0].equals(tag)) {
26 view.setBackgroundColor(Color.BLUE);
27 } else if (tabTitle[1].equals(tag)) {
28 view.setBackgroundColor(Color.CYAN);
29 } else if (tabTitle[2].equals(tag)) {
30 view.setBackgroundColor(Color.DKGRAY);
31 } else if (tabTitle[3].equals(tag)) {
32 view.setBackgroundColor(Color.GRAY);
33 } else if (tabTitle[4].equals(tag)) {
34 view.setBackgroundColor(Color.GREEN);
35 } else if (tabTitle[5].equals(tag)) {
36 view.setBackgroundColor(Color.LTGRAY);
37 } else if (tabTitle[6].equals(tag)) {
38 view.setBackgroundColor(Color.MAGENTA);
39 } else if (tabTitle[7].equals(tag)) {
40 view.setBackgroundColor(Color.RED);
41 } else if (tabTitle[8].equals(tag)) {
42 view.setBackgroundColor(Color.YELLOW);
43 }
44 return view;
45 }
46 }

对应的layout的xml如下:

1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:orientation="vertical" >
6
7 <TabHost
8 android:id="@android:id/tabhost"
9 android:layout_width="match_parent"
10 android:layout_height="match_parent" >
11
12 <LinearLayout
13 android:layout_width="match_parent"
14 android:layout_height="match_parent"
15 android:orientation="vertical" >
16
17 <TabWidget
18 android:id="@android:id/tabs"
19 android:layout_width="wrap_content"
20 android:layout_height="wrap_content" >
21 </TabWidget>
22
23 <FrameLayout
24 android:id="@android:id/tabcontent"
25 android:layout_width="match_parent"
26 android:layout_height="match_parent" >
27 </FrameLayout>
28 </LinearLayout>
29 </TabHost>
30
31 </LinearLayout>


1 <?xml version="1.0" encoding="utf-8"?>
2 <TextView xmlns:android="http://schemas.android.com/apk/res/android"
3 android:id="@+id/tv_title"
4 android:layout_width="wrap_content"
5 android:layout_height="wrap_content"
6 android:layout_gravity="center"
7 android:gravity="center"
8 android:singleLine="true" />

运行后的效果图如下(平板1280*800的设备):
第二步,给标签栏添加横向的滚动操作
这个很简单,只需要修改一下tabhost.xml即可:

1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:orientation="vertical" >
6
7 <TabHost
8 android:id="@android:id/tabhost"
9 android:layout_width="match_parent"
10 android:layout_height="match_parent" >
11
12 <LinearLayout
13 android:layout_width="match_parent"
14 android:layout_height="match_parent"
15 android:orientation="vertical" >
16
17 <HorizontalScrollView
18 android:layout_width="fill_parent"
19 android:layout_height="wrap_content"
20 android:fillViewport="true"
21 android:scrollbars="none" >
22
23 <TabWidget
24 android:id="@android:id/tabs"
25 android:layout_width="wrap_content"
26 android:layout_height="wrap_content" >
27 </TabWidget>
28 </HorizontalScrollView>
29
30 <FrameLayout
31 android:id="@android:id/tabcontent"
32 android:layout_width="match_parent"
33 android:layout_height="match_parent" >
34 </FrameLayout>
35 </LinearLayout>
36 </TabHost>
37
38 </LinearLayout>

运行效果如下:
第三步,修改标签的宽度和高度
这里要改2个地方,一个是activity中,一个是tabwighet.xml中;
首先说明一下tabwighet.xml修改,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:orientation="vertical" > <TextView
android:id="@+id/tv_title"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:gravity="center"
android:singleLine="true" /> </LinearLayout>

其中需要注意一点,设置标签的宽度和高度不能设置到LinearLayout上,否则设置的宽度和高度不起作用。
Activity的修改就很简单了,根据tabwighet.xml的修改稍微调整一下代码即可,修改的代码如下:
1 LinearLayout view = (LinearLayout) getLayoutInflater().inflate(
2 R.layout.tabwighet, null);
3 ((TextView) view.findViewById(R.id.tv_title)).setText(tabTitle[i]);
运行的效果如下:
第四步,修改标签的样式,增加背景图片和选中的状态,以及选中后的字体的颜色
首先修改tabhost.xml文件,为HorizontalScrollView和FrameLayout添加设置background的属性,图片自己选个即可。代码如下:

1 <HorizontalScrollView
2 android:layout_width="fill_parent"
3 android:layout_height="wrap_content"
4 android:background="@drawable/ic_tab_header_background"
5 android:fillViewport="true"
6 android:scrollbars="none" >
7
8 <TabWidget
9 android:id="@android:id/tabs"
10 android:layout_width="wrap_content"
11 android:layout_height="wrap_content" >
12 </TabWidget>
13 </HorizontalScrollView>
14
15 <FrameLayout
16 android:id="@android:id/tabcontent"
17 android:layout_width="match_parent"
18 android:layout_height="match_parent"
19 android:background="@drawable/ic_tab_body_background" >
20 </FrameLayout>

然后在drawable文件夹中添加2个selector,一个是用来设置选中的标签的背景的,一个是用来设置选中的字体颜色变化的,代码如下:
1 <?xml version="1.0" encoding="utf-8"?>
2 <selector xmlns:android="http://schemas.android.com/apk/res/android">
3 <item android:drawable="@drawable/tabbar_bg_selected" android:state_selected="true"></item>
4 <item android:drawable="@android:color/transparent" android:state_selected="false"></item>
5 </selector>
1 <?xml version="1.0" encoding="utf-8"?>
2 <selector xmlns:android="http://schemas.android.com/apk/res/android">
3 <item android:color="#9d9d9d" android:state_selected="false"></item>
4 <item android:color="@android:color/black" android:state_selected="true"></item>
5 </selector>
最后修改tabwighet.xml将样式添加到标签上:

1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="wrap_content"
4 android:layout_height="wrap_content"
5 android:layout_gravity="center_horizontal"
6 android:background="@drawable/selector_tab_header_item_background"
7 android:gravity="center_horizontal"
8 android:orientation="vertical" >
9
10 <TextView
11 android:id="@+id/tv_title"
12 android:layout_width="200dp"
13 android:layout_height="50dp"
14 android:layout_gravity="center"
15 android:gravity="center"
16 android:singleLine="true"
17 android:textColor="@drawable/selector_tab_header_item_txt_color" />
18 </LinearLayout>

运行效果如下:
第五步,添加tabhost中标签间的分割线
修改tabhost.xml的代码为:
<TabWidget
android:id="@android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="@drawable/ic_tabbar_divider" >
</TabWidget>
效果图如下:
以上几步就完成了我们预定的3个功能。
下面是我在开发中遇到的几个特使的问题,总结一下并列出我的解决方法:
1.标签的宽度总是设置不成功——请参照第三步操作,这里注意设置最外边的view的宽度是不能控制标签的宽度的。
2.标签间设置的自定义分割图片总是不显示——检查AndroidManifest.xml中是不是设置了application的android:theme属性,如果设置成了@android:style/Theme.NoTitleBar,那么设置的分割是不能正常显示的(初步测试设置了与NoTitleBar有关的样式的属性都不能显示),如果设置这个属性是为了隐藏actionbar,建议修改成@android:style/Theme.DeviceDefault.NoActionBar。
Android中TabHost中实现标签的滚动以及一些TabHost开发的奇怪问题的更多相关文章
- Android TabHost中实现标签的滚动以及一些TabHost开发的奇怪问题
最近在使用TabHost的时候遇到了一些奇怪的问题,在这里总结分享备忘一下. 首先说一点TabActivity将会被FragmentActivity所替代,但是本文中却是使用的TabActivity. ...
- Android中仿淘宝首页顶部滚动自定义HorizontalScrollView定时水平自动切换图片
Android中仿淘宝首页顶部滚动自定义HorizontalScrollView定时水平自动切换图片 自定义ADPager 自定义水平滚动的ScrollView效仿ViewPager 当遇到要在Vie ...
- Android 自定义View修炼-Android中常见的热门标签的流式布局的实现
一.概述:在日常的app使用中,我们会在android 的app中看见 热门标签等自动换行的流式布局,今天,我们就来看看如何 自定义一个类似热门标签那样的流式布局吧(源码下载在下面最后给出哈) 类似的 ...
- Android中全屏 取消标题栏,TabHost中设置NoTitleBar的三种方法(转)
Android中全屏 取消标题栏,TabHost中设置NoTitleBar的三种方法http://www.cnblogs.com/zdz8207/archive/2013/02/27/android- ...
- Android manifest文件中的标签详细介绍
官方文档 概要 每一个Android应用都应该包含一个manifest文件,即AndroidManifest.xml.它包含了程序运行的一些必备信息,比如:--为Java应用程序指定一个独一无二的名字 ...
- (备忘)android清单文件中<meta-data>标签,以及<intent-filter>下的<data>标签及其他标签
1.metadata可以写在application下也可以写在activity下,作为全局或activity内共享的数据 以键值对形式保存 <meta-data android:name=&qu ...
- Android TabHost中Activity之间传递数据
例子1: TabHost tabhost = (TabHost) findViewById(android.R.id.tabhost); tabhost.setup(this.getLocalActi ...
- 移动端网站如何开发(电脑端网站到手机端网站我们需要在html代码中添加哪个meta标签)
移动端网站如何开发(电脑端网站到手机端网站我们需要在html代码中添加哪个meta标签) 一.总结 一句话总结: 添加viewport标签:meta name="viewport" ...
- iOS / Android 移动设备中的 Touch Icons
上次转载了一篇<将你的网站打造成一个iOS Web App>,但偶然发现这篇文章的内容有些是错误的——准确来说也不是错误,只是不适合自半年前来的情况了(也可以说是iOS7 之后的时间)—— ...
随机推荐
- js进阶 13-1 jquery动画中的显示隐藏函数有哪些
js进阶 13-1 jquery动画中的显示隐藏函数有哪些 一.总结 一句话总结:show(),hide(),toggle(),这三个. 1.jquery动画中显示隐藏效果函数有哪些? show()h ...
- 关于Altium Designer的BOM,元件清单
在生成BOM列表的时候,要记得调整BOM的表格的宽度,以免显示不全, 还有就是BOM列表一共有 comment栏 ,description栏,designator栏,footprint栏,libref ...
- 前端js常用正则表达式实例讲解
本文内容整理自他人优秀的博客,非纯原创.仅借此学习和整理. 1.匹配用户名 规则描述: 长度4-6位: {4,16} 字母: [a-z] [A-Z] 数字: [0-9] 下划线: [_] 减号: [- ...
- 中间件、服务器和Web服务器三者的区别
相信很多的Web安全初学者和我一样,对中间件和服务器的认识不够深刻,对两者的概念可能会有所混淆. 正好今天在学习的时候突然想到了这个问题,粗略百度了一下,似乎网上对这个问题的解释不多,那么就由我来为大 ...
- 三个Bootstrap免费字体和图标库
前言:Bootstrap 简洁.直观.强悍.移动设备优先的前端开发框架,让web开发更迅速.简单 ,深入了解 Bootstrap 底层结构的关键部分,包括我们让 web 开发变得更好.更快, 组件无数 ...
- Qt浅谈之一:内存泄露(总结),对于QWidget可以setAttribute(Qt::WA_DeleteOnClose),而且绝对不能手动删除栈上的对象
一.简介 Qt内存管理机制:Qt 在内部能够维护对象的层次结构.对于可视元素,这种层次结构就是子组件与父组件的关系:对于非可视元素,则是一个对象与另一个对象的从属关系.在 Qt 中,在 Qt 中,删除 ...
- 【u121】教主的花园
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 教主有着一个环形的花园,他想在花园周围均匀地种上n棵树,但是教主花园的土壤很特别,每个位置适合种的树都 ...
- [D3] Create Labels from Non-numeric Data with Ordinal Scales in D3 v4
When your data contains discrete, non-numeric property values that you need to format or convert bef ...
- jquery-11 留言板如何实现
jquery-11 留言板如何实现 一.总结 一句话总结:用live()方法让后面动态添加的元素也绑定之前对应类绑定的方法. 1.如何让后面动态添加的元素也绑定之前对应类绑定的方法? 用live()方 ...
- javaScript_with用法
with语句用途 暂时改变作用域链.简化代码. 语法结构 with(object){ //其他语句 } 例1 with(person){ name= "zhang"; addres ...