如何在所选选项卡上设置 ActionBar、选项卡背景的样式 [英] How to Style ActionBar, tab background on selected tab
问题描述
我正在努力设计 ActionBar 的样式.我的应用程序有一个带有三个选项卡的 ActionBar.我试图让选定的选项卡具有背景颜色,并使未选定的选项卡显示不同的颜色.我正在关注此参考:自定义操作栏.但是所有的 TAB 都显示了 Selected 颜色.
I'm struggling with styling the ActionBar. My app has an ActionBar with three tabs. I'm trying to get the selected tab to have a background color, and the unselected tabs to show a different color. I'm following this reference: Customizing Action Bar. But all the TABs are showing the Selected color.
我的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
) 继承了操作栏选项卡.我已经看过了参考文献 & 很难理解样式层次结构
tab_background is just a 9 patch. I'm also not sure if I'm inheriting the action bar tab from the correct parent (parent="android:style/Widget.Holo.Light.ActionBar.TabBar
). I've looked through the references & find it very difficult to understand the style hierarchy
为什么我的标签页不显示选中或不显示?预先感谢您的帮助.
Why wont my tabs show selected or no? Thanks in advance for your assistance.
推荐答案
我解决了我的问题.我最初没有使用 State List Drawables.我直接使用了背景图像,而不是通过 StateListDrawable.使用StateListDrawable,你可以根据标签是否被选中来设置不同的背景.
I solved my problem. I didn't use State List Drawables initially. I used the background image directly instead of going via the StateListDrawable. Using StateListDrawable, you can set a different background based on whether the tag is selected or not.
所以我添加了文件 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
中引用了这个:
and I referenced this from my styles.xml
:
<item name="android:background">@drawable/tab_background_select</item>
这篇关于如何在所选选项卡上设置 ActionBar、选项卡背景的样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!