Android TabHost中实现标签的滚动以及一些TabHost开发的奇怪问题
最近在使用TabHost的时候遇到了一些奇怪的问题,在这里总结分享备忘一下。
首先说一点TabActivity将会被FragmentActivity所替代,但是本文中却是使用的TabActivity。
下面说说本程序能够实现的功能:
- 实现TabHost中的标题栏能够横向滚动;
- 自定义标题栏的大小和样式;
- 自定义标题栏的分割线的样式;
下面分几步来分别实现以上的功能:
第一步,先实现一个基本的TabHost的展现
详细的说明可以在网上其它地方搜的,主要就是注意一点,控件的id的是固定的不能随便更改,并且@和id之间不能加+;
Activity的代码如下:
public class TabhostTestActivity extends TabActivity implements
TabContentFactory {
private final String[] tabTitle = { "测试Tab标签1", "测试Tab标签2", "测试Tab标签3",
"测试Tab标签4", "测试Tab标签5", "测试Tab标签6", "测试Tab标签7", "测试Tab标签8",
"测试Tab标签9" }; /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabhost);
TabHost th = getTabHost();
for (int i = ; i < tabTitle.length; i++) {
TextView view = (TextView) getLayoutInflater().inflate(
R.layout.tabwighet, null);
view.setText(tabTitle[i]);
th.addTab(th.newTabSpec(tabTitle[i]).setIndicator(view)
.setContent(this));
}
} @Override
public View createTabContent(String tag) {
View view = new View(this);
if (tabTitle[].equals(tag)) {
view.setBackgroundColor(Color.BLUE);
} else if (tabTitle[].equals(tag)) {
view.setBackgroundColor(Color.CYAN);
} else if (tabTitle[].equals(tag)) {
view.setBackgroundColor(Color.DKGRAY);
} else if (tabTitle[].equals(tag)) {
view.setBackgroundColor(Color.GRAY);
} else if (tabTitle[].equals(tag)) {
view.setBackgroundColor(Color.GREEN);
} else if (tabTitle[].equals(tag)) {
view.setBackgroundColor(Color.LTGRAY);
} else if (tabTitle[].equals(tag)) {
view.setBackgroundColor(Color.MAGENTA);
} else if (tabTitle[].equals(tag)) {
view.setBackgroundColor(Color.RED);
} else if (tabTitle[].equals(tag)) {
view.setBackgroundColor(Color.YELLOW);
}
return view;
}
}
对应的layout的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" > <TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TabWidget
android:id="@android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TabWidget> <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
</TabHost> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:singleLine="true" />
运行后的效果图如下(平板1280*800的设备):


第二步,给标签栏添加横向的滚动操作
这个很简单,只需要修改一下tabhost.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" > <TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none" > <TabWidget
android:id="@android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TabWidget>
</HorizontalScrollView> <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
</TabHost> </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的修改稍微调整一下代码即可,修改的代码如下:
LinearLayout view = (LinearLayout) getLayoutInflater().inflate(
R.layout.tabwighet, null);
((TextView) view.findViewById(R.id.tv_title)).setText(tabTitle[i]);
运行的效果如下:

第四步,修改标签的样式,增加背景图片和选中的状态,以及选中后的字体的颜色
首先修改tabhost.xml文件,为HorizontalScrollView和FrameLayout添加设置background的属性,图片自己选个即可。代码如下:
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/ic_tab_header_background"
android:fillViewport="true"
android:scrollbars="none" > <TabWidget
android:id="@android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TabWidget>
</HorizontalScrollView> <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ic_tab_body_background" >
</FrameLayout>
然后在drawable文件夹中添加2个selector,一个是用来设置选中的标签的背景的,一个是用来设置选中的字体颜色变化的,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/tabbar_bg_selected" android:state_selected="true"></item>
<item android:drawable="@android:color/transparent" android:state_selected="false"></item>
</selector>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#9d9d9d" android:state_selected="false"></item>
<item android:color="@android:color/black" android:state_selected="true"></item>
</selector>
最后修改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:background="@drawable/selector_tab_header_item_background"
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"
android:textColor="@drawable/selector_tab_header_item_txt_color" />
</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. ...
- 如何将Eclipse中的项目迁移到Android Studio 中
如何将Eclipse中的项目迁移到Android Studio 中 如果你之前有用Eclipse做过安卓开发,现在想要把Eclipse中的项目导入到Android Studio的环境中,那么首先要做的 ...
- Android 布局 中实现适应屏幕大小及组件滚动
要实现如图的布局: 这是在eclipse可视化窗口中的截图,但实际运行在Android设备上可能出现的问题有: (1):当编辑图1中的最后一个EditText时,输入法的编辑界面会把底部的Button ...
- android中include标签使用详解
android中include标签是为了便于控件的覆用的一个很好解决方案. 但是也有一些需要注意的地方,下面是本人在项目中碰到过的一个问题,做此记录,便于以后查看. include标签用法. ...
- 【Android】AndroidManifest 中original-package标签
Manifest.xml 中 <manifest>标签中package属性用于设置应 用程序的进程名,即在运行时使用ddms查看到的进程名. <original-packag ...
- Android中全屏 取消标题栏,TabHost中设置NoTitleBar的三种方法(转)
Android中全屏 取消标题栏,TabHost中设置NoTitleBar的三种方法http://www.cnblogs.com/zdz8207/archive/2013/02/27/android- ...
- Android TabHost中Activity之间传递数据
例子1: TabHost tabhost = (TabHost) findViewById(android.R.id.tabhost); tabhost.setup(this.getLocalActi ...
- Android application 和 activity 标签详解
extends:http://blog.csdn.net/self_study/article/details/54020909 Application 标签 android:allowTaskRep ...
- Android开发的小技巧,在Android Studio中使用Designtime Layout Attributes
在编写xml文件时,为了预览效果,经常会使用默认填上一些内容,比如TextView时,随便写上一个text <TextView ... android:text="Name:" ...
随机推荐
- OpenCV基本架构[OpenCV 笔记0]
最近正在系统学习OpenCV,将不定期发布笔记,主要按照毛星云的<OpenCV3编程入门>的顺序学习,会参考官方教程和文档.学习工具是Xcode+CMake,会对书中一部分内容更正,并加入 ...
- 重学C++ (1)
写在开头的话:这学期没有写太多的代码,终于把中英文两篇论文弄完了,趁着中间的空隙,想想找工作的处境.自己也定了自己的方向.不管学什么语言吧,每个语言都有自己的优势和使用的群体.只要自己是良马,终会有伯 ...
- 线程模式HS/HA和L/F的区别, HS/HA的实现原理图
线程池模式一般分为两种:L/F领导者与跟随者模式.HS/HA半同步/半异步模式. HS/HA 半同步/ 半异步模式 :分为三层,同步层.队列层.异步层,又称为生产者消费者模式,主线程处理I/O事件并解 ...
- Linux防火墙基本知识
一.防火墙的分类 (一).包过滤防火墙. 数据包过滤(packet Filtering)技术是在网络层对数据包进行选择,选择的依据是系统内设置的过滤逻辑,称为访问控制表(access control ...
- JSON C# Class Generator ---由json字符串生成C#实体类的工具
json作为互联网上轻量便捷的数据传输格式,越来越受到重视.但在服务器端编程过程中,我们常常希望能通过智能提示来提高编码效率.JSON C# Class Generator 能将json格式所表示的J ...
- JS判断设备终端(PC,iPad,iPhone,android,winPhone)和浏览器
JS判断设备终端(PC,iPad,iPhone,android,winPhone)和浏览器 var ua = navigator.userAgent; var browser = {}, weixin ...
- Django Tutorial 学习笔记
实际操作了Django入门教程中的范例,对一些细节有了更清晰的掌握.感觉只看文档不动手是不行的,只看文档没法真正掌握其中要素之间的关系,看了很多遍也不行,必须动手做了才能掌握.同时,这次练习在Ecli ...
- Spark小课堂Week1 Hello Spark
Spark小课堂Week1 Hello Spark 看到Spark这个词,你的第一印象是什么? 这是一朵"火花",官方的定义是Spark是一个高速的.通用的.分布式计算系统!!! ...
- FPGA初学心得
有三种方法在模块中产生逻辑:1.使用连续赋值语句“assign”:2.用实例元件 3.用“always”块.所以在always块中赋值不能使用assign,而是直接给变量赋值就行. reg与wire的 ...
- php使用注意点
php使用时间之前要将php.ini中时区设置好,否则会报警告.截图如下:“;date.timezone =”设置为“date.timezone =Asia/Shanghai”即可. apache如果 ...