更新Android的标签图标 [英] Updating Android Tab Icons

查看:139
本文介绍了更新Android的标签图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有具有含一组TabSpecs每个包含的项目的选项卡中显示的列表视图一个TabHost的活动。当创建每个则tabspec,予置的图标要被显示在选项卡头

I have an activity which has a TabHost containing a set of TabSpecs each with a listview containing the items to be displayed by the tab. When each TabSpec is created, I set an icon to be displayed in the tab header.

该TabSpecs在一个 setupTabs()方法中这样循环到创建标签适当数量的创建:

The TabSpecs are created in this way within a setupTabs() method which loops to create the appropriate number of tabs:

TabSpec ts = mTabs.newTabSpec("tab");
ts.setIndicator("TabTitle", iconResource);

ts.setContent(new TabHost.TabContentFactory(
{
    public View createTabContent(String tag)
    {
        ... 
    }            
});
mTabs.addTab(ts);

有,我希望能够改变这是我的程序的执行过程中显示在每个标签的图标一对夫妇的实例。目前,我删除所有选项卡,并再次呼吁上述code来重新创建它们。

There are a couple instances where I want to be able to change the icon which is displayed in each tab during the execution of my program. Currently I am deleting all the tabs, and calling the above code again to re-create them.

mTabs.getTabWidget().removeAllViews();
mTabs.clearAllTabs(true);
setupTabs();

有没有一种方法,以取代正在显示,而不删除并重新创建所有标签的​​图标?

Is there a way to replace the icon that is being displayed without deleting and re-creating all of the tabs?

推荐答案

简短的回答是,你不会错过任何东西。 Android的SDK不提供直接的方法更改 TabHost 的指示它创建后,真实。该则tabspec 仅用于构建的标签,所以改变了则tabspec

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.

我觉得有一个解决办法,但。呼叫 mTab​​s.getTabWidget()获得 TabWidget 对象。这是的ViewGroup 只是一个子类,这样你就可以调用 getChildCount() getChildAt( )来在 TabWidget 访问各个选项卡。每个标签也是一个视图,并与一个图形标志和文字标签选项卡的情况下,几乎可以肯定一些其他的的ViewGroup (也许的LinearLayout ,但它并不重要),其中包含一个的ImageView 的TextView 。因此只要摆弄与调试器或 Log.i ,你应该能够找出一个配方,以获得的ImageView 键,直接改变它。

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.

这篇关于更新Android的标签图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆