`
superhanliu
  • 浏览: 49582 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

android tabhost 图标

阅读更多

http://www.aslibra.com/blog/post/TabHost_addTab_TabSpec_change_icon.php

 

做了个tab形式的界面,但一直找不到系统提供的接口怎么修改图标,因为当前选择的和未选择的会改变。

 

添加tab的代码其实很简单

 

        TabHost mTabHost = getTabHost();

 

        TabHost.TabSpec tab_category = mTabHost.newTabSpec(tab_tag[0]);

        tab_category.setIndicator(tabIndex[0], getResources().getDrawable(R.drawable.ic_tab_selected_contacts));

        tab_category.setContent(R.id.layout_category);

  mTabHost.addTab(tab_category);

 

//其它tab类似

 

  //监听tab点击事件

  mTabHost.setOnTabChangedListener(this);

 

 

setIndicator可以设置tab的标题和图标。

可是,会发现个问题,onTabChanged函数里,并不能如自己想的那样改变icon:

 

比如,你找到tab_category对象后,这样的操作无效:

tab_category.setIndicator(tabIndex[0], getResources().getDrawable(R.drawable.ic_tab_unselected_contacts));

 

这挺烦人的,可也不是没有办法,很难得居然搜到了答案:

 

引用

The short answer is, you're not missing anything. The Android SDK doesn't provide a direct method to change the indicator of a TabHost after it's been created. The TabSpec is only used to build the tab, so changing the TabSpec after the fact will have no effect.

 

I think there's a workaround, though. Call mTabs.getTabWidget() to get a TabWidget object. This is just a subclass of ViewGroup, so you can call getChildCount() and getChildAt() to access individual tabs within the TabWidget. Each of these tabs is also a View, and in the case of a tab with a graphical indicator and a text label, it's almost certainly some other ViewGroup (maybe a LinearLayout, but it doesn't matter) that contains an ImageView and a TextView. So with a little fiddling with the debugger or Log.i, you should be able to figure out a recipe to get the ImageView and change it directly.

 

The downside is that if you're not careful, the exact layout of the controls within a tab could change and your app could break. Your initial solution is perhaps more robust, but then again it might lead to other unwanted side effects like flicker or focus problems.

 

 

不翻译了,我的英文并不是很好,大家参考一下就好。

大致是说的TabSpec并没有提供方法修改icon,但此是View的扩展类,可以根据组织规律找到该icon的,就可以改掉它了。

还有示例代码:

 

tabHost.setOnTabChangedListener(new OnTabChangeListener() {

                public void onTabChanged(String tabId) {

                        if (TAB_MAP.equals(tabId)) {

                                ImageView iv = (ImageView)tabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.icon);

                                iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_map_black));

                                iv = (ImageView)tabHost.getTabWidget().getChildAt(1).findViewById(android.R.id.icon);

                                iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_list_white));

                        } else if (TAB_LIST.equals(tabId)) {

                                ImageView iv = (ImageView)tabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.icon);

                                iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_map_white));

                                iv = (ImageView)tabHost.getTabWidget().getChildAt(1).findViewById(android.R.id.icon);

                                iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_list_black));

                        }

                }

        });

 

 

这个代码阿权证实是可行的,很高兴能够实现自己的想法!

 

参考网址: Updating Android Tab Icons 

 

原创内容如转载请注明:来自 阿权的书房 

本帖地址:http://www.aslibra.com/blog/post/TabHost_addTab_TabSpec_change_icon.php

分享到:
评论

相关推荐

    Android TabHost选项卡标签图标始终不出现的解决方法

    本文实例分析了Android TabHost选项卡标签图标始终不出现的解决方法。分享给大家供大家参考,具体如下: 在学习Android TabHost布局过程中,很多教程告诉我,这样来显示选项卡标签的图标和文字: TapSpec spec1 = ...

    Android TabHost实现屏幕UI窗口的TAB切换功能.rar

    Android TabHost实现屏幕UI窗口的TAB切换功能,实现的过程:声明TabHost对象、取得TabHost对象,为TabHost添加标签,新建一个newTabSpec(newTabSpec),设置其标签和图标(setIndicator),设置内容(setContent),然后...

    android中TabHost的图标(48×48)和文字叠加解决方法

    开发过程中,有时候图标稍微大点,比如48×48的时候,文字就会和图标叠加起来,遇到这种问题我们该怎样处理呢?本文将详细介绍希望对你有所帮助

    Android TabHost UI设计 菜单完美显示

    Android,TabHos的UI设计,菜单效果完美显示,模板作用。

    Android为TabHost添加标签的实现.rar

    android 为TabHost添加标签的实现源码,新建一个newTabSpec(newTabSpec),设置其标签和图标(setIndicator),设置内容(setContent),设置TabHost的背景颜色,设置TabHost的背景图片资源,mTabHost....

    android中TabHost的图标(4848)和文字叠加解决方法

    开发过程中,有时候图标稍微大点,比如48×48的时候,文字就会和图标叠加起来,解决方法如下: 代码如下:TabWidget tw = tabHost.getTabWidget(); for (int i = 0; i < tw.getChildCount(); i++) { TextView tv=...

    标题带图标和文字的TabHost

    本实例是一个简单的TabHost控件应用,其中标题是同时显示图标和文字的。希望能给初学者带来帮助。

    android的Tag标签的实现

    import android.widget.TabHost.OnTabChangeListener; public class Activity01 extends TabActivity { //声明TabHost对象 TabHost mTabHost; @Override public void onCreate(Bundle savedInstanceState) { ...

    android百度api2.1

    位置标注:提供一个或多个POI位置标注,且支持用户自定义图标。 实时路况:提供城市实时交通路况信息图。 离线地图:提供离线地图功能,可节省用户流量。 定位:采用GPS、WIFI、基站、IP混合定位模式,请使用Android...

    Android 完美高仿的微信源码

    本软件基本实现微信的所有UI效果。具备如下功能点: ...摇一摇判断“shake”部分来源于网络,具体地址忘了。。。。摇一摇的动画效果是我做的,支持真机摇一摇哦! 新特点:主界面使用的是...(比原版微信tabhost强多了!)

    Android开发资料合集-World版!

    4.7、TAB与TABHOST 106 4.8、RATINGBAR 110 4.8.1、例一 110 4.8.2、例二 112 4.9、DATE/TIME SET 115 4.9.1、DatePicker/TimePicker 115 4.9.2、DatePickerDialog/TimePickerDialog 119 4.10、WEBVIEW 120 4.10.1、...

    Android 开发技巧

    4.7、TAB与TABHOST 106 4.8、RATINGBAR 110 4.8.1、例一 110 4.8.2、例二 112 4.9、DATE/TIME SET 115 4.9.1、DatePicker/TimePicker 115 4.9.2、DatePickerDialog/TimePickerDialog 119 4.10、WEBVIEW 120 4.10.1、...

    Android铃声设置模块

    摘要:Java源码,Android源码,铃声设置 Android铃声设置模块源码,通过本Andorid源码将了解以下相关技巧: 声明和取得TabHost对象、新建一个newTabSpec(newTabSpec)、设置其标签和图标(setIndicator)、设置内容...

    Android开发资料合集--续

    33、使用系统自带的TabHost的问题 59 34、弹出菜单 61 35、Toast重叠显示时延迟解决 62 36、ADT新特性:ImageView的定义 62 37、MotionEvent 中获取坐标的问题 63 38、添加多个Widget样式 63 39、为Activity添加快捷...

    黑马程序员 安卓学院 万元哥项目经理 分享220个代码实例

    |--TabHost一个界面显示多Activity |--TextView单行跑马灯效果 |--TextView虚拟获得焦点 |--uploadServlet |--uri之表示资源resource |--ViewPage的使用 |--view中的tag用法之存储对象 |--view常用属性 |--xml常用...

    百度地图毕业设计源码-birthday:一个安卓应用程序

    2.加入了cx_faapp_icon.png的android图标 3.创建测试版本 版本2 1.mainactivity界面的设计---->此处用tabhost来实现 2.tabs搞定了 3.明天思考下mainactivity的background是不是需要的 2014年4月23日 版本1 1.早上...

    标签有凸起的

    在网上看到凸起的tab,就是中间有个凸起的图标;自己参考别人写的,仅供参考。

    TabActivity的选项卡演示

    继承TabActivity的选项卡显示,本演示中同样创建了普通选项卡,图标选项卡以及选项卡的内嵌;

Global site tag (gtag.js) - Google Analytics