无法使用AppCompat设置ActionBar选项卡指示器的样式

时间:2013-12-30 17:14:35

标签: android tabs android-actionbar android-actionbar-compat android-appcompat

我一直在尝试使用AppCompat库自定义Android ActionBar选项卡上的蓝色指示器但没有成功。我可以更改标签栏的背景颜色,但似乎themes.xml完全忽略了android:actionBarTabStyle属性。我定义了一组9补丁图像,一个基于状态的可绘制XML文件。

搜索Google还没有解决我的问题,我希望尽可能少地使用依赖项,所以我宁愿不使用ActionBarSherlock,除非它是最后的手段。

预期结果:使用9补丁图像,标签指示符应为橙色。 Screenshot of tab bar. Tab indicator is orange

实际结果标签指示器仍为蓝色 Screenshot of tab bar. Tab indicator is blue

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>

任何人都可以看到此代码的任何明显问题,以及为什么它不起作用?非常感谢。

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,当我从常规代码中拆分支持兼容性代码时,它有效。将常规代码放在目录values-v14/themes.xml中。这将使版本14及以上使用常规声明(例如android:background)和14及以下使用支持compat代码(例如background)。希望这很清楚