2016-06-13 418 views
1

我的选项卡文本是蓝色,背景是白色。选择选项卡时,我想显示蓝色背景和白色标签文本。更改选项卡选择选项卡时的文本颜色

我使用选择器更改了背景。但我多次尝试选择时文本颜色不会改变。请帮助我。

styles.xml

<style name="MainTheme" parent="android:Theme.Holo.Light.DarkActionBar"> 
    <item name="android:actionBarTabTextStyle">@style/tab_text_color</item> 
</style> 

<style name="actionbar_tab_style" parent="@android:style/Widget.Holo.Light.ActionBar.TabBar"> 
    <item name="android:background">@drawable/tab_background_select</item> 
    <item name="android:textColor">@drawable/tab_text_select</item> 
</style> 

tab_background_select.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item android:state_selected="true" 
     android:drawable="@color/blue"/> 
</selector> 

tab_text_select.xml

<?xml version="1.0" encoding="utf-8"?> 

<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item 
     android:state_selected="true" 
     android:color="#FFFFFF"/> 
    <item 
     android:state_selected="false" 
     android:color="#0000FF"/> 
     <!-- 
     <item android:state_selected="true"> 
      <shape> 
       <solid android:color="@color/white"/> 
      </shape> 
     </item> 
     <item> 
      <shape> 
       <solid android:color="@color/light_blue"/> 
      </shape> 
     </item> --> 

</selector> 
+0

我假设你正在使用'TabLayout'。你在哪里定义你的'TabLayout'?请显示XML代码,以便我们可以检查它。 – ishmaelMakitla

+0

我在代码 – tenten

+0

中使用了操作栏选项卡,并且您知道这种导航方式已被[在API级别21中弃用](https://developer.android.com/reference/android/app/ ActionBar.Tab.html)?在任何情况下,你可以尝试像这样:'getActionBar()。setStackedBackgroundDrawable(new ColorDrawable(Color.parseColor(“#YourFavoriteColorHere”)))' - 也看看这个密切相关的讨论[这里](http:// stackoverflow.com/questions/11318750/change-actionbar-tabs-background-color)。 – ishmaelMakitla

回答

0

我找到了答案。我必须将tab_text_select.xml放在ti res/colors文件夹中(如果您没有该文件夹,只需创建它)。

然后

<style name="actionbar_tab_text_style" parent="@android:style/Widget.Holo.Light.ActionBar.TabText"> 
<item name="android:textColor">@color/tab_background_select</item> 

然后上面的样式应用到主题名称为android:actionBarTabTextStyle

相关问题