我试图更改TabWidget
文本颜色,但没有成功,即使我尝试了不同的方法来更改它(请参见下面的代码)。
我的背景选项卡是一个图像:
for (int i = 0; i < tabHost.getTabWidget().getTabCount(); i++) {
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
}
我不知道这是否与我现在想做的事情产生了某种冲突。
解决方案1:
MIN
....
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/tabbarbackground"
android:tabStripEnabled="false"
style="@style/TabText"
/> ....
XML
... <style name="TabText">
<item name="android:textColor">@color/tab_text_color</item> </style> ....
制表符文本颜色.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:textColor="#2daed9" />
<item android:state_selected="false" android:color="#FFFFFF" />
</selector>
解决方案2
for (int i = 0; i < tabHost.getTabWidget().getTabCount(); i++) {
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
RelativeLayout rl = (RelativeLayout) tabHost.getTabWidget().getChildAt(i);
TextView textView = (TextView) rl.getChildAt(1);
textView.setTextColor(R.color.tab_text_color);
}
制表符文本颜色.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:textColor="#2daed9" />
<item android:state_selected="false" android:color="#FFFFFF" /> </selector>
但这两种解决方案都行不通。
但是,如果我改变第二个解决方案
textView.setTextColor (R.color.tab_text_color);
到
textView.setTextColor (Color.parseColor ("# ...."));
它可以工作,除了这个解决方案不改变颜色的文字时,我点击它。
谢谢。
最佳答案
我能解决,解决方案不优雅,但工作。我希望对某人有用的人:
首先,我必须为所有选项卡的textview设置init color:
for (int i = 0; i < tabHost.getTabWidget().getTabCount(); i++) {
vg = (ViewGroup) getTabHost().getTabWidget().getChildAt(i);
tv = (TextView) vg.getChildAt(1);
tv.setTypeface(font);
if (i == 0) {
tv.setTextColor(Color.parseColor("#2daed9"));
Currentab = 0;
} else {
tv.setTextColor(R.color.GrisOscuro);
}
}
然后,我在override方法ontabchanged中设置每个选项卡的更改颜色。tab脉冲是i(gettabhost().getcurrenttab())。最后一个标签是currentab。
getTabHost().setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String tabId) {
int i = getTabHost().getCurrentTab();
if (Currentab != i) {
vg = (ViewGroup) getTabHost().getTabWidget()
.getChildAt(Currentab);
tv = (TextView) vg.getChildAt(1);
tv.setTextColor(R.color.GrisOscuro);
Currentab = i;
vg = (ViewGroup) getTabHost().getTabWidget()
.getChildAt(i);
tv = (TextView) vg.getChildAt(1);
tv.setTextColor(Color.parseColor("#2daed9"));
}
}
});
对不起我的英语,我希望对某人有用=)再见!D