文本颜色不会更改TabWidget

时间:2012-06-25 15:58:46

标签: android android-tabhost textview android-textattributes

我正在尝试更改TabWidget文字颜色,但没有成功,即使我尝试了不同的方法来更改它(请参阅下面的代码。)

我的背景标签是图片:

for (int i = 0; i < tabHost.getTabWidget().getTabCount(); i++) {
    tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.TRANSPARENT); 
}

我不知道这是否与我现在想要做的事情产生某种冲突。

解决方法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 name="TabText">
    <item name="android:textColor">@color/tab_text_color</item> </style> ....

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

<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 ("# ....")); 

它有效,但是当我点击它时,此解决方案不会改变文本的颜色。

感谢。

3 个答案:

答案 0 :(得分:6)

我能够解决,解决方案不优雅但有效。我希望谁对某人有用:

首先,我必须为所有标签的textview设置初始颜色:

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中设置了每个选项卡的更改颜色。脉冲的标签是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

答案 1 :(得分:4)

解决方案2

TabWidget tabwidget=mTabHost.getTabWidget();
for(int i=0;i<tabwidget.getChildCount();i++){
    TextView tv=(TextView) tabwidget.getChildAt(i).findViewById(android.R.id.title);
    tv.setTextColor(this.getResources().getColorStateList(R.color.tab_text_xml));
}

答案 2 :(得分:1)

尝试编写这个方法:

public void onTabChanged(String tabId) {

for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
TextView tv = (TextView) tabhost.getTabWidget()
.getChildAt(i).findViewById(R.id.your_text_id);
    tv.setTextColor(#FFFFFF);

}

TextView tv = (TextView)    tabHost.getTabWidget().
getChildAt(tabHost.getCurrentTab()).findViewById(R.id.your_text_id);

tv.setTextColor(#2daed9);
}