Java 更新 Android 标签图标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Updating Android Tab Icons
提问by lnediger
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.
我有一个活动,它有一个 TabHost,其中包含一组 TabSpecs,每个 TabSpecs 都有一个包含要由选项卡显示的项目的列表视图。创建每个 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 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?
有没有办法在不删除和重新创建所有选项卡的情况下替换正在显示的图标?
采纳答案by dmazzoni
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.
简短的回答是,您没有遗漏任何东西。Android SDK 不提供直接方法来在 aTabHost
创建后更改指示符。将TabSpec
只用于建立标签,所以改变TabSpec
的事实后不会有任何效果。
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.
不过,我认为有一种解决方法。调用mTabs.getTabWidget()
以获取TabWidget
对象。这只是 的一个子类ViewGroup
,因此您可以调用getChildCount()
和getChildAt()
访问TabWidget
. 这些选项卡中的每一个也是一个视图,并且在带有图形指示符和文本标签的选项卡的情况下,几乎可以肯定是其他一些ViewGroup
(可能是LinearLayout
,但无关紧要)包含ImageView
和TextView
。因此,稍微摆弄一下调试器 or 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.
不利的一面是,如果您不小心,选项卡内控件的确切布局可能会发生变化,并且您的应用程序可能会中断。您最初的解决方案可能更强大,但它可能会再次导致其他不需要的副作用,例如闪烁或聚焦问题。
回答by srakyi
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() 中使用这些直接索引一点也不好......
回答by srakyi
See my post with code example regarding Customized Android Tabs.
请参阅我的帖子,其中包含有关自定义 Android 选项卡的代码示例。
Thanks Spct
谢谢 SPCT
回答by srakyi
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
回答by Mohit
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);
回答by Gautam Vasoya
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));
}
}
});
回答by Gautam Vasoya
We can use thread look at this link
我们可以使用线程查看此链接
http://groups.google.com/group/android-developers/browse_thread/thread/ef3bdebcb715b385