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

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

问题描述

我有一个活动,它有一个 TabHost,其中包含一组 TabSpecs,每个 TabSpecs 都有一个包含要由选项卡显示的项目的列表视图.创建每个 TabSpec 时,我设置了一个图标以显示在选项卡标题中.

I have an activity that 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);

在某些情况下,我希望能够在程序执行期间更改每个选项卡中显示的图标.目前,我正在删除所有选项卡,并再次调用上述代码以重新创建它们.

There are a couple of 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.

不过,我认为有一个解决方法.调用 mTabs.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天全站免登陆