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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值