如何设置ActionBar样式,选中选项卡上的选项卡背景

Coc*_*ess 17 android android-actionbar android-tabs android-styles

我正在努力设计ActionBar.我的应用程序有一个带有三个选项卡的ActionBar.我试图让选定的标签具有背景颜色,而未选择的标签显示不同的颜色.我正在关注此参考:自定义操作栏.但是所有TAB都显示了选定的颜色.

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

tab_background只是一个9补丁.我也不确定我是否从正确的parent(parent="android:style/Widget.Holo.Light.ActionBar.TabBar)继承了操作栏选项卡.我查看了参考资料,发现很难理解样式层次结构

为什么我的标签显示不选择?提前感谢你的帮助.

Coc*_*ess 11

我解决了我的问题.我最初没有使用State List 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>
Run Code Online (Sandbox Code Playgroud)

我从我这里引用了这个styles.xml:

 <item name="android:background">@drawable/tab_background_select</item>
Run Code Online (Sandbox Code Playgroud)

  • 你是什​​么意思从styles.xml引用了这个?你是如何引用它以及以何种方式引用它的?我怎么做?我无法让它发挥作用.对此没有任何作用,无论我做什么,它都不会改变.Android似乎真的很麻烦.你引用了什么父母?你叫它什么名字?你在清单中提到了什么? (4认同)