我不知道我做了什么,但有一段时间我的TabWidget的标签是白色的,看起来非常好。我从未在我的项目中设置过主题或背景/前景颜色。下一次编译它时,它就恢复为灰色标签了。我的应用程序正在使用默认的暗色主题。即使我将应用程序主题设置为亮色,标签仍然是灰色的。因此,显然是其他东西改变了标签的颜色。有人知道如何做到这一点吗?
我遇到了一个问题,这是由于Android 1.6的浅色主题造成的(选项卡指示器文本为白色)。我按照以下方式覆盖了默认主题:
styles.xml
:
<style name="MyTheme" parent="@android:style/Theme.Light">
<item name="android:tabWidgetStyle">@style/LightTabWidget</item>
</style>
<style name="LightTabWidget" parent="@android:style/Widget.TabWidget">
<!-- set textColor to red, so you can verify that it applied. -->
<item name="android:textColor">#f00</item>
</style>
然后我只需通过将 android:theme="@style/MyTheme"
添加到我的 AndroidManifest.xml
文件中的 <application />
元素来将该主题应用于我的应用程序。
android.graphics.drawable
包。tabHost.getTabWidget().getChildAt(0).setBackgroundResource(
android.R.color.white);
在 public void onCreate(Bundle savedInstanceState)
中
`tabHost = getTabHost();
tabHost.setOnTabChangedListener(this);
tabHost.setCurrentTab(0);
setTabColor();`
在监听器中:
public void onTabChanged(String tabId) { setTabColor();
最后是设置前景和背景的函数:
public void setTabColor() {
// set foreground color:
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
RelativeLayout rl = (RelativeLayout) tabHost.getTabWidget().getChildAt(i);
ImageView imageView = (ImageView) rl.getChildAt(0);// change it if you want it
TextView textView = (TextView) rl.getChildAt(1);//
textView.setTextColor(Color.parseColor("#FFFFFF"));
}
// set background color:
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#010101")); // unselected
}
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#121288")); // selected
}
在 onCreated() 中:
tabHost.setCurrentTab(0);
// Set tabs text color to white:
TabWidget tabWidget = tabHost.getTabWidget();
int whiteColor = getResources().getColor(R.color.white);
int someOtherColor = getResources().getColor(R.color.someOtherColor);
for(int i = 0; i < tabWidget.getChildCount(); i++){
View tabWidgetChild = tabWidget.getChildAt(i);
if(tabWidgetChild instanceof TextView){
((TextView) tabWidgetChild).setTextColor(whiteColor);
} else if(tabWidgetChild instanceof Button){
((Button) tabWidgetChild).setTextColor(whiteColor);
} else if(tabWidgetChild instanceof ViewGroup){
ViewGroup vg = (ViewGroup)tabWidgetChild;
for(int y = 0; y < vg.getChildCount(); y++){
View vgChild = vg.getChildAt(y);
if(vgChild instanceof TextView){
((TextView) vgChild).setTextColor(whiteColor);
}
}
vg.setBackgroundColor(someOtherColor);
}
}
DDMS
拍摄的屏幕截图,那将非常有帮助。 - Roman NuriktargetSdkVersion
属性导致它对我进行更改。 - Steve Pomeroy