2012-11-08 51 views
16

我很努力与样式的ActionBar。我的应用程序有一个带三个选项卡的ActionBar。我试图让选定的标签具有背景颜色,而未选中的标签显示不同的颜色。我遵循这个参考:Customizing Action Bar。但是所有的TAB都显示了“选定的颜色”。如何样式的ActionBar,在选定的选项卡上的标签背景

styles.xml文件如下:

<style name="MyActionBarTabStyle" parent="android:style/Widget.Holo.Light.ActionBar.TabBar"> 
    <item name="android:background">@drawable/tab_background</item> 
    <item name="android:paddingLeft">32dp</item> 
    <item name="android:paddingRight">32dp</item> 
</style> 

<style name="MyActionBarTabBarStyle" parent="android:style/Widget.Holo.Light.ActionBar.TabBar"> 

    <item name="android:background">@drawable/red</item> 
</style> 


<style name="AppTheme.Light" parent="@android:style/Theme.Holo.Light"> 
    <item name="android:actionBarStyle">@style/ActionBar.Light</item> 
    <item name="android:actionBarTabStyle">@style/MyActionBarTabStyle</item> 
    <item name="android:actionBarTabBarStyle">@style/MyActionBarTabBarStyle</item> 

</style> 

tab_background只是一个9补丁。我也不确定是否继承了正确父级的操作栏选项卡(parent="android:style/Widget.Holo.Light.ActionBar.TabBar)。我查看了参考文献&发现很难理解风格层次结构

为什么我的选项卡不显示选择或不显示?提前感谢你的帮助。

+0

使用[ActionBarSherlockStyleGenerator](http://jgilfelt.github.com/android-actionbarstylegenerator/ #name = example&compat = holo&theme = light&actionbarstyle = solid&backColor = E4E4E4,100&secondaryColor = D6D6D6,100&tertiaryColor = F2F2F2,100&accentColor = 33B5E5,100)来设计和使用。检查我的答案[这里](http://stackoverflow.com/questions/13269482/change-background-of-the-actionbarsherlock-alone-not-the-tabs/13283261#13283261) – PravinCG

+0

谢谢普拉万。 ActionBarStyleGenerator对于ActionBar风格非常有用 – CocoNess

回答

10

我解决了我的问题。我最初并没有使用状态列表Drawables。我直接使用背景图像,而不是通过StateListDrawable。使用StateListDrawable,您可以根据是否选择标记来设置不同的背景。

所以我添加的文件tab_background_select.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_selected="true" 
     android:drawable="@drawable/tab_background" /> 

</selector> 

,我引用这个从我styles.xml

<item name="android:background">@drawable/tab_background_select</item> 
+0

谢谢Dude;)+1 –

+3

你是什么意思你从styles.xml引用了这个?你是如何参考它以什么方式参考的?我怎么做?我无法让它工作。没有任何工作为此,不管我做什么,它都不会改变。 Android看起来真的很麻烦。你提到什么家长?你叫什么名字?你在清单中提到了什么? –

+1

不错的答案:)使用像这样,' – tenten

相关问题