[英]Unable to style ActionBar tab indicator using AppCompat
我一直在尝试使用AppCompat库自定义Android ActionBar选项卡上的蓝色指示器,但未成功。 我可以更改标签栏的背景颜色,但是看起来themes.xml完全忽略了android:actionBarTabStyle
属性。 我定义了一组9个补丁图像,一个基于状态的可绘制XML文件。
搜索Google仍无法解决我的问题,我想使用尽可能少的依赖项,因此除非万不得已,否则我不愿使用ActionBarSherlock。
预期结果:使用9色块图像,选项卡指示器应为橙色。
实际结果:选项卡指示器仍为全息蓝色
tab_indicator.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected_orange" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_selected_orange" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected_focused_orange" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_selected_focused_orange" />
<!-- Pressed -->
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_unselected_pressed_orange" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed_orange" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_unselected_pressed_orange" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed_orange" />
</selector>
的themes.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="style/Theme.AppCompat">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="android:actionBarStyle">@style/ActionBarTheme</item>
<!-- Support library compatibility -->
<item name="actionBarStyle">@style/ActionBarTheme</item>
</style>
<!-- ActionBar styles -->
<style name="ActionBarTheme" parent="@style/Widget.AppCompat.ActionBar">
<item name="android:background">@color/titleBackgroundColour</item>
<item name="android:backgroundStacked">@color/tabBackground</item>
<item name="android:textColor">@color/titleTextColour</item>
<item name="android:titleTextStyle">@style/ActionBarTitleTextStyle</item>
<item name="android:actionBarTabStyle">@style/ThemeTabStyle</item>
<!-- Support library compatibility -->
<item name="actionBarTabStyle">@style/ThemeTabStyle</item>
</style>
<!-- ActionBar tabs styles -->
<style name="ThemeTabStyle" parent="@style/Widget.AppCompat.ActionBar.TabView">
<!-- tab indicator -->
<item name="android:background">@drawable/tab_indicator</item>
<!-- Support library compatibility -->
<item name="background">@drawable/tab_indicator</item>
</style>
</resources>
谁能看到此代码有任何明显的问题,为什么它不起作用? 非常感谢。
我遇到了同样的问题,当我从常规代码中拆分支持兼容性代码时,它起作用了。 将常规代码放在目录values-v14/themes.xml
。 这将使版本14及更高版本使用正则声明(例如android:background
),而版本14及以下版本使用支持compat代码(例如background
)。 希望这足够清楚
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.