相当郁闷的问题,TabHost选项卡标签图标始终不出现?
在学习Android TabHost布局过程中,很多教程告诉我,这样来显示选项卡标签的图标和文字:
TapSpec spec1 = tabHost.newTabSpec("tab 1");
spec1.setIndicator("选项卡一", getResources().getDrawable(R.drawable.tab_icon));
spec1.setContent(R.id.tab1);
tabHost.addTab(spec1);
折腾来折腾去,setIndicator(label, drawable)这个方法始终不能将标题文字与图标一起显示出来,只有文字标题。
在没将电脑砸了之前,通过万能的stackoverflow.com终于知道确切答案以及相应方法了:
http://stackoverflow.com/questions/10745092/icon-in-tab-is-not-showing-up
其实就是SDK 4.03(冰激凌)下:只有文字标题显示,图标是不显示的。如果将文字标题设置为空字符串,则此时图标可显示。
对于冰激凌下两全其美的方法,只能是自定义标签卡布局,创建一个包含ImageView和TextView组件的界面布局文件 tab_indicator.xml(layout/tab_indicator.xml),然后用setIndicator(View view)方法来设置TabSpec的界面布局。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dip"
android:layout_height="64dip"
android:layout_weight="1"
android:orientation="vertical"
android:background="@drawable/tab_indicator"
android:padding="5dp"> <ImageView android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
/> <TextView android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
style="?android:attr/tabWidgetStyle"
/>
</RelativeLayout>

接着我们可以在drawable图片资源目录下创建一个tab_info.xml文件,用来指示Tab图标的各状态。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/tab_info_dark"
android:state_selected="true" />
<item android:drawable="@drawable/tab_info_light" />
</selector>

现在就可以通过下面的代码将我们自定义的视图作为一个indicator配置给TapSpec对象。
private void addTab(String label, int drawableId) {Intent intent = new Intent(this, MockActivity.class);TabHost.TabSpec spec = tabHost.newTabSpec(label);<span style="color: #ff0000;">View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);</span>TextView title = (TextView) tabIndicator.findViewById(R.id.title);title.setText(label);ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);icon.setImageResource(drawableId);<span style="color: #ff0000;">spec.setIndicator(tabIndicator);</span>spec.setContent(intent);tabHost.addTab(spec);} |
像以下方式那样调用上面那自定义addTab方法
tabHost = getTabHost(); //tabHost is a private field
addTab("First", R.drawable.tab_info);
addTab("Second", R.drawable.tab_info);
addTab("Third", R.drawable.tab_info);
注意:当用自定义视图的indicator来添加Tab时,要将strip_enabled属性设置为false。若要兼顾底部strip,那在添加最后一个Tab后设置getTabWidget().setStripEnabled(true);
相当郁闷的问题,TabHost选项卡标签图标始终不出现?的更多相关文章
- jQuery---bootstrap的下载使用,栅格(12个格子),轮播图,矢量图字体图标,进度条,选项卡,标签,表达校验
jQuery---bootstrap的下载使用,栅格(12个格子),轮播图,矢量图字体图标,进度条,选项卡,标签,表达校验 一丶bootstrap的使用 下载资源文件: bootstrap官网 导入 ...
- Android TabHost 动态修改图标或者动态改变标题
那时客户需要实现在TabHost标题上动态显示从数据库获取的个数.起初这样思考的,从数据库 获取个数是非常简单,但是要把获取的个数显示在TabHost标题,思前想后,想用Handler来异步实现消息传 ...
- 【Android 应用开发】Android - TabHost 选项卡功能用法详解
TabHost效果图 : 源码下载地址 : http://download.csdn.net/detail/han1202012/6845105 . 作者 :万境绝尘 转载请注明出处 ...
- Android - TabHost 选项卡功能用法详解
TabHost效果图 : 源码下载地址 : http://download.csdn.net/detail/han1202012/6845105 . 作者 :万境绝尘 转载请注明出处 ...
- [Xcode 实际操作]三、视图控制器-(2)UITabBarController选项卡(标签)视图控制器
目录:[Swift]Xcode实际操作 本文将为你演示,选项卡视图控制器的创建和使用. 在项目文件夹[DemoApp]上点击鼠标右键,弹出右键菜单. [New File]->[Cocoa Tou ...
- TabHost选项卡的实现(二):使用Fragment实现
在上一篇博客<TabHost选项卡的实现(一):使用TabActivity实现>中,讲解了如何使用TabActivity创建管理选项卡,但是,通过TabActivity创建选项卡的方式已经 ...
- Android ViewPager实现Tabhost选项卡底部滑块动态滑动过渡
<Android ViewPager实现Tabhost选项卡底部滑块动态滑动过渡> 之前基于github上的第三方开源控件ViewPagerIndicator的UnderlinePa ...
- android学习--TabHost选项卡组件
TabHost是一种非常有用的组件,TabHost能够非常方便地在窗体上放置多个标签页,每一个标签页获得了一个与外部容器同样大小的组件摆放区域.在手机系统的应用类似"未接电话".& ...
- TabHost选项卡的实现(一):使用TabActivity实现
一. TabHost的基本开发流程 TabHost是一种非常实用的组件,可以很方便的在窗口上防止多个标签页,每个标签页相当于获得了一个外部容器相同大小的组件摆放区域. 我们熟悉的手机电话系统" ...
随机推荐
- Python实践练习目录
缘由 做中学才是最好的方法,通过这些项目来加强自己的Python掌握程度. 原则 成体系地学,不搞"题海战术" 通所有不如精一物,精一物方可通所有 走心学,忌浮躁 项目列表 字符串 ...
- 由于安装Android设备驱动异常,ADB无法识别安卓设备的解决方案
体验更优排版请移步原文:http://blog.kwin.wang/programming/android-driver-exception-solution.html 最近换了台新电脑,在Andro ...
- python算法之希尔排序
希尔排序 希尔排序(Shell Sort)是插入排序的一种.也称缩小增量排序,是直接插入排序算法的一种更高效的改进版本.希尔排序是非稳定排序算法.该方法因DL.Shell于1959年提出而得名. 希尔 ...
- ZTree 获取选中的项,jQuery radio的取值与赋值
$("input[name='Sex']:checked").val();//取值 $("input[name='radioName'][value=2]"). ...
- 搭建Git Server - 个人开发简单搭建
###################### 教程一 ####################### 1. 创建git用户和用户组 #新建一个git用户组 sudo groupadd git #新建一 ...
- Visual Studio Find All no results.
重装WDK什么的有时候有bug....写下面注册表修复 Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Wow6432Node\CLSI ...
- What is _MainTex_ST ?
[What is _MainTex_ST ?] 在Unity自带的Unlit/Texture中,有引用到float4 _MainTex_ST,如下: // Unlit shader. Simplest ...
- 111. Minimum Depth of Binary Tree (Tree; DFS)
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- 英文单词cipher 和password的区别,用法有什么不同,
['saɪfə(r)] cipher 指一套密码系统,比如电影<风声>中破译的那个系统叫cipher:password 则指进入的指令,比如你的qq密码,电脑密码等叫password.总之 ...
- php设置错误,错误记录
//设置错误级别. error_reporting(E_ALL); //显示所有错误 error_reporting(E_ALL&~E_NOTICE); //显示所有错误但不显示提示级别的 ...