简体   繁体   English

更新 Android 标签图标

[英]Updating Android Tab Icons

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.我有一个活动,它有一个 TabHost,其中包含一组 TabSpecs,每个 TabSpecs 都有一个包含要由选项卡显示的项目的列表视图。 When each TabSpec is created, I set an icon to be displayed in the tab header.创建每个 TabSpec 时,我设置了一个要显示在选项卡标题中的图标。

The TabSpecs are created in this way within a setupTabs() method which loops to create the appropriate number of tabs: TabSpecs 在setupTabs()方法中以这种方式创建,该方法循环以创建适当数量的选项卡:

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?有没有办法在不删除和重新创建所有选项卡的情况下替换正在显示的图标?

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. Android SDK 不提供直接方法来在创建TabHost后更改其指示器。 The TabSpec is only used to build the tab, so changing the TabSpec after the fact will have no effect. TabSpec仅用于构建选项卡,因此事后更改TabSpec将不起作用。

I think there's a workaround, though.不过,我认为有一个解决方法。 Call mTabs.getTabWidget() to get a TabWidget object.调用mTabs.getTabWidget()来获取一个TabWidget对象。 This is just a subclass of ViewGroup , so you can call getChildCount() and getChildAt() to access individual tabs within the TabWidget .这只是ViewGroup一个子类,因此您可以调用getChildCount()getChildAt()来访问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 .这些选项卡中的每一个也是一个视图,在带有图形指示器和文本标签的选项卡的情况下,几乎可以肯定是其他一些包含ImageViewTextView ViewGroup (可能是LinearLayout ,但无所谓) . 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.因此,稍微摆弄调试器或Log.i ,您应该能够找出获取ImageView并直接更改它的方法。

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.您最初的解决方案可能更强大,但它可能会再次导致其他不需要的副作用,例如闪烁或聚焦问题。

Just to confirm dominics answer, here's his solution in code (that actually works):只是为了确认多米尼克的回答,这是他的代码解决方案(实际上有效):

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));
        }
    }
});

Of course it's not polished at all and using those direct indices in getChildAt() is not nice at all...当然,它根本没有经过打磨,在 getChildAt() 中使用这些直接索引一点也不好......

It might be a little late but you might wanna take a look at this thread 可能会有点晚,但您可能想看看这个话题

http://groups.google.com/group/android-developers/browse_thread/thread/ef3bdebcb715b385 http://groups.google.com/group/android-developers/browse_thread/thread/ef3bdebcb715b385

See my post with code example regarding Customized Android Tabs .请参阅我的帖子,其中包含有关自定义 Android 选项卡的代码示例。

Thanks Spct谢谢 SPCT

This is what I did and it works for me.这就是我所做的,它对我有用。 I created this function in the activity that extends from TabBarActivity我在从 TabBarActivity 扩展的活动中创建了这个函数

public void updateTab(int stringID) {
    ViewGroup identifyView = (ViewGroup)getTabWidget().getChildAt(0);
    TextView v =  (TextView)identifyView.getChildAt(identifyView.getChildCount() - 1);
    v.setText(stringID);
}

You can modify this function to change the image instead of text or you can change both, also you can modify this to get any tab child.您可以修改此功能以更改图像而不是文本,也可以更改两者,也可以修改此功能以获取任何选项卡子项。 I was particularly interested in modifying the text of the first tab at runtime.我对在运行时修改第一个选项卡的文本特别感兴趣。

I called this function from the relevant activity using this call我使用此调用从相关活动中调用了此函数

getParent().updateTab(R.string.tab_bar_analyze);

Try This:试试这个:

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));
        }
    }
});

我们可以用线程看看这个链接

http://groups.google.com/group/android-developers/browse_thread/thread/ef3bdebcb715b385

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM