问题描述
我试图修改 TabWidget
文本颜色,都没有成功,即使我已经尝试了不同的方式来改变它(见下文code)。
I'm trying to change the TabWidget
text color, without success, even though I've tried different way to change it (see code below.)
我的背景标签是图像:
for (int i = 0; i < tabHost.getTabWidget().getTabCount(); i++) {
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
}
我不知道这是否产生某种冲突与我想现在做的事。
I do not know if this creates some sort of conflict with what I want to do now.
解决方法1:
main.xml中
....
<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"
/> ....
style.xml
style.xml
... <style name="TabText">
<item name="android:textColor">@color/tab_text_color</item> </style> ....
tab_text_color.xml
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>
解决方案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);
}
tab_text_color.xml
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>
但既不解决方案工作。
but neither solution works.
但是,如果我更改第二个解决方案
However, if I change the second solution
textView.setTextColor (R.color.tab_text_color);
到
textView.setTextColor (Color.parseColor ("# ...."));
它的工作原理,但这种解决方案并不改变文本的颜色,当我点击它。
It works, except this solution does not change the color of text when I click on it.
感谢。
推荐答案
我能够解决的,解决的办法是不优雅,但工程。我希望谁是有用的人:
I was able to solve, the solution isn't elegant but works. I hope who is usefull for somebody:
首先,我必须设置初始化颜色所有选项卡的TextView的:
First, I must set init color for the textview of all tabs:
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);
}
}
然后,我在重写方法设置ontabchanged,每个标签的变色。脉冲的标签是我(getTabHost()。getCurrentTab())。又是谁我preSS最后一个标签是Currentab。
And then, I set in the override method ontabchanged, the change color for each tab. The tab pulsed are i (getTabHost().getCurrentTab()). And the last tab who I press is 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
Sorry for my english, I hope is useful for somebody =) Bye! ;D
这篇关于文字颜色没有变化TabWidget的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!