【问题标题】:Unable to style ActionBar tab indicator using AppCompat无法使用 AppCompat 设置 ActionBar 选项卡指示器的样式
【发布时间】:2013-12-30 17:14:35
【问题描述】:

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

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

预期结果:标签指示器应该是橙色的,使用 9-patch 图像。

实际结果:标签指示器仍然是全息蓝色

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>

谁能看到这段代码有任何明显的问题,为什么它不起作用?非常感谢。

【问题讨论】:

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


    【解决方案1】:

    我遇到了同样的问题,当我将支持兼容性代码与常规代码分开时,它就起作用了。将常规代码放在目录values-v14/themes.xml 中。这将使版本 14 及更高版本使用常规声明(例如 android:background),而 14 及以下版本使用支持兼容代码(例如 background)。希望这足够清楚

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-20
      • 2012-04-11
      • 1970-01-01
      相关资源
      最近更新 更多