如何更改ActionbarTab的选定选项卡颜色或字体颜色?

时间:2014-01-29 06:39:57

标签: android xml tabs

我正在使用xml来使用带有视图寻呼机的Actionbar标签更改所选标签的背景 代码如下,

style.xml

<!-- Custom theme. -->
<style name="MyTheme" parent="android:style/Theme.Holo.Light">
    <item name="android:windowActionBar">true</item>
    <item name="android:actionBarSize">30dp</item>
    <item name="android:icon">@drawable/logo</item>
    <item name="android:actionBarTabTextStyle">@style/Widget.Holo.TabWidget</item>
     <!-- <item name="android:actionBarTabTextStyle">@style/customTabBar</item>   -->

</style>
<style name="Widget.Holo.TabWidget" parent="android:style/Widget.TabWidget">
    <item name="android:tabStripLeft">@null</item>
    <item name="android:tabStripRight">@null</item>
    <item name="android:tabStripEnabled">true</item>
    <!-- <item name="android:divider">@android:attr/dividerVertical</item> -->
    <item name="android:showDividers">middle</item>
    <item name="android:dividerPadding">8dip</item>
     <item name="android:divider">@android:attr/dividerVertical</item>
     <item name="android:dividerHeight">10dp</item>
    <item name="android:measureWithLargestChild">true</item>        
    <item name="android:textSize">16sp</item>
    <item name="android:textStyle">bold</item>  
    <item name="android:background">@drawable/tab_bg_selector</item>

</style>

tab_bg_selector.xml

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

<!-- Active tab -->

<item android:drawable="@drawable/tab_bg_selected" android:state_focused="false" android:state_pressed="false" android:state_selected="true"/>

<!-- Inactive tab 

<item android:drawable="@drawable/tab_bg_unselected" android:state_focused="false" android:state_pressed="false" android:state_selected="false"/>

- &GT;     

<item android:drawable="@android:color/transparent" android:state_pressed="true"/>

<!-- Selected tab (using d-pad) -->

<item android:drawable="@android:color/transparent" android:state_focused="true" android:state_pressed="false" android:state_selected="true"/>

tab_bg_selected.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

<gradient
    android:centerColor="#7F7F7F"
    android:endColor="#696969"
    android:startColor="#A8A8A8" />

 </shape>

它显示我改变了颜色,但颜色只是文本背景的变化我想要改变整个选项卡

见下图,

  

enter image description here

2 个答案:

答案 0 :(得分:2)

这些自定义样式适用于我

 <style name="MyActionBarTabStyle" parent="@style/Widget.AppCompat.Light.ActionBar.TabView">
    <item name="android:tabStripEnabled">false</item>
    <item name="android:footerDividersEnabled">false</item>
    <item name="android:background">@drawable/tab_selector</item>
    <item name="background">@drawable/tab_selector</item>
</style>

<style name="MyActionBarTabTextStyle" parent="@style/Widget.AppCompat.Light.ActionBar.TabText">
    <item name="android:textColor">@color/tab_text_color</item>
    <item name="android:textSize">12sp</item>
    <item name="android:gravity">fill_horizontal</item>
</style>

我希望它对你也适用

答案 1 :(得分:0)

看看这个:Styling Tabs