小编rom*_*ben的帖子

文本颜色不会更改TabWidget

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

我的背景标签是一张图片:

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

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

解决方法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"
        /> ....
Run Code Online (Sandbox Code Playgroud)

style.xml

... <style name="TabText">
    <item name="android:textColor">@color/tab_text_color</item> </style> ....
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

解决方案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);
}
Run Code Online (Sandbox Code Playgroud)

tab_text_color.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:textColor="#2daed9" /> …
Run Code Online (Sandbox Code Playgroud)

android textview android-tabhost android-textattributes

2
推荐指数
1
解决办法
7635
查看次数