更新 Android 选项卡图标
-
09-06-2019 - |
题
我有一个活动,其中有一个 TabHost,其中包含一组 TabSpec,每个 TabSpec 都有一个列表视图,其中包含选项卡要显示的项目。创建每个 TabSpec 时,我设置了一个要在选项卡标题中显示的图标。
TabSpecs 以这种方式在一个 setupTabs()
循环创建适当数量的选项卡的方法:
TabSpec ts = mTabs.newTabSpec("tab");
ts.setIndicator("TabTitle", iconResource);
ts.setContent(new TabHost.TabContentFactory(
{
public View createTabContent(String tag)
{
...
}
});
mTabs.addTab(ts);
在某些情况下,我希望能够更改程序执行期间每个选项卡中显示的图标。目前我正在删除所有选项卡,并再次调用上面的代码来重新创建它们。
mTabs.getTabWidget().removeAllViews();
mTabs.clearAllTabs(true);
setupTabs();
有没有办法替换正在显示的图标,而无需删除并重新创建所有选项卡?
解决方案
简短的回答是,你没有错过任何东西。Android SDK没有提供直接的方法来改变a的指示器 TabHost
创建后。这 TabSpec
仅用于构建选项卡,因此更改 TabSpec
事后将没有任何影响。
不过,我认为有一个解决方法。称呼 mTabs.getTabWidget()
得到一个 TabWidget
目的。这只是一个子类 ViewGroup
, ,所以你可以打电话 getChildCount()
和 getChildAt()
访问各个选项卡 TabWidget
. 。这些选项卡中的每一个也是一个视图,对于带有图形指示器和文本标签的选项卡来说,它几乎肯定是其他一些 ViewGroup
(也许是一个 LinearLayout
, ,但这并不重要)包含一个 ImageView
和一个 TextView
. 。因此,稍微摆弄一下调试器或者 Log.i
, ,你应该能够找出一个食谱来获得 ImageView
并直接更改它。
缺点是,如果您不小心,选项卡中控件的确切布局可能会发生变化,并且您的应用程序可能会崩溃。您最初的解决方案可能更可靠,但它可能会导致其他不需要的副作用,例如闪烁或聚焦问题。
其他提示
只是为了确认多米尼克的答案,这是他的代码解决方案(实际上有效):
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));
}
}
});
当然,它根本没有完善,并且在 getChildAt() 中使用这些直接索引一点也不好......
请参阅我的帖子,其中包含有关的代码示例 定制 Android 选项卡.
谢谢Spct
这就是我所做的,它对我有用。我在从 TabBarActivity 扩展的活动中创建了这个函数
public void updateTab(int stringID) {
ViewGroup identifyView = (ViewGroup)getTabWidget().getChildAt(0);
TextView v = (TextView)identifyView.getChildAt(identifyView.getChildCount() - 1);
v.setText(stringID);
}
您可以修改此函数来更改图像而不是文本,或者您可以更改两者,也可以修改此函数以获取任何选项卡子项。我对在运行时修改第一个选项卡的文本特别感兴趣。
我使用此调用从相关活动中调用了此函数
getParent().updateTab(R.string.tab_bar_analyze);
尝试这个:
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));
}
}
});