【问题标题】:How to change background of tab indicator image?如何更改选项卡指示器图像的背景?
【发布时间】:2013-03-01 03:21:35
【问题描述】:
这是一张图片。
我想要这样的tabview图像背景。当它没有被选中时,它显示白色图像,当它被选中时,它显示绿色图像。
代码如下:
Resources resources = getResources();
TabHost tabhost = getTabHost();
Intent one = new Intent().setClass(this, StyleMe.class);
TabSpec tb1=tabhost.newTabSpec("One").setIndicator("",resources.getDrawable
(R.drawable.style_link)).setContent(one);
tabhost.addTab(tb1);
【问题讨论】:
标签:
android-layout
android-widget
android-tabhost
android-button
android-icons
【解决方案1】:
更好的解决方案是使用带有选择器的背景,这样您就不必检查 onTabChanged 并进行手动更新。:
private void initTabsAppearance(TabWidget tabWidget) {
// Change background
for(int i=0; i < tabWidget.getChildCount(); i++)
tabWidget.getChildAt(i).setBackgroundResource(R.drawable.tab_bg);
}
其中 tab_bg 是带有选择器的 xml 可绘制对象:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/tab_bg_selected" />
<item android:drawable="@drawable/tab_bg_normal" />
</selector>
希望这会有所帮助。