操作栏样式不会覆盖操作栏选项卡的样式

时间:2014-09-29 11:23:16

标签: android android-actionbar android-styles android-actionbar-compat

我使用下面的代码更改应用程序中选项卡式活动的样式:

<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="Theme.AppCompat.Light">
    <!--
        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="actionBarStyle">@style/ActionBarTheme</item>
</style>

<!-- Action Bar them -->

<style name="ActionBarTheme" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="background">@drawable/ab_solid_appolo</item>
    <item name="backgroundSplit">@drawable/ab_bottom_solid_appolo</item>
    <item name="titleTextStyle">@style/ActionBarText</item>
    <item name="icon">@drawable/ic_action_launcher</item>
    <item name="actionBarTabStyle">@style/ActionBarTabStyle.Appolo</item>
</style>

<style name="ActionBarText" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">#000000</item>
</style>

<!-- ActionBar tabs styles -->
<style name="ActionBarTabStyle.Appolo" parent="@style/Widget.AppCompat.Light.ActionBar.TabView">
    <item name="background">@drawable/actionbar_tab_indicator</item>
</style>

这是actionbar_tab_indicator内容:

    <?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="@android:color/transparent" />
    <item android:state_focused="false" android:state_selected="true"  android:state_pressed="false" android:drawable="@drawable/tab_selected" />

    <!-- Focused states -->
    <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected_focused" />
    <item android:state_focused="true" android:state_selected="true"  android:state_pressed="false" android:drawable="@drawable/tab_selected_focused" />

    <!-- Pressed -->
    <!--    Non focused states -->
    <item android:state_focused="false" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_unselected_pressed" />
    <item android:state_focused="false" android:state_selected="true"  android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed" />

    <!--    Focused states -->
    <item android:state_focused="true" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_unselected_pressed" />
    <item android:state_focused="true" android:state_selected="true"  android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed" />
</selector>

我还在style-v11和style-v14中使用下面的代码:

...
      <style name="ActionBarTheme" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="background">@drawable/ab_solid_appolo</item>
        <item name="backgroundSplit">@drawable/ab_bottom_solid_appolo</item>
        <item name="android:titleTextStyle">@style/ActionBarText</item>
        <item name="android:icon">@drawable/ic_action_launcher</item>
        <item name="android:actionBarTabStyle">@style/ActionBarTabStyle.Appolo</item>
    </style>

    <style name="ActionBarText" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
        <item name="android:textColor">#000000</item>
    </style>

   <!-- ActionBar tabs styles -->
    <style name="ActionBarTabStyle.Appolo" parent="@style/Widget.AppCompat.Light.ActionBar.TabView">


        <item name="android:background">@drawable/actionbar_tab_indicator</item>
    </style>
...

但它并没有改变标签栏的风格。

我多次查看我的代码,搜索并查看了所有笔记,但我无法找到问题所在。有没有人有任何答案?

4 个答案:

答案 0 :(得分:1)

问题在于这部分代码:

<style name="ActionBarTheme" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="background">@drawable/ab_solid_appolo</item>
<item name="backgroundSplit">@drawable/ab_bottom_solid_appolo</item>
<item name="titleTextStyle">@style/ActionBarText</item>
<item name="icon">@drawable/ic_action_launcher</item>
<item name="actionBarTabStyle">@style/ActionBarTabStyle.Appolo</item>

特别是这一行:

<item name="actionBarTabStyle">@style/ActionBarTabStyle.Appolo</item>

这一行不应该在我想用作动作栏主题的自定义中。正确的方法是在我的自定义应用下添加它们,以便从上方删除此行并将其添加到AppBaseTheme下:

<style name="AppTheme" parent="AppBaseTheme">

    <!-- All customizations that are NOT specific to a particular API-level can go here. -->

    <item name="actionBarStyle">@style/ActionBarTheme</item>
     <item name="actionBarTabStyle">@style/ActionBarTabStyle.Appolo</item>
</style>

现在代码工作正常。

答案 1 :(得分:0)

试试这个:

<style name="ActionBarTheme" parent="@style/Theme.AppCompat.Light">
    <item name="android:actionBarStyle">@style/ActionBarTheme.ActionBarStyle</item>
</style>

<style name="ActionBarTheme.ActionBarStlye" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:background">@drawable/ab_solid_appolo</item>
    <item name="android:backgroundSplit">@drawable/ab_bottom_solid_appolo</item>
    <item name="android:titleTextStyle">@style/ActionBarTheme.ActionBar.TitleTextStyle</item>
    <item name="android:icon">@drawable/ic_action_launcher</item>
    <item name="android:actionBarTabStyle">@style/ActionBarTheme.ActionBar.TabStyle</item>
</style>

<style name="ActionBarTheme.ActionBar.TitleTextStyle" parent="@style/TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:textColor">#000000</item>
</style>

<!-- ActionBar tabs styles -->
<style name="ActionBarTheme.ActionBar.TabStyle" parent="@style/Widget.Holo.Light.ActionBar">
    <item name="android:background">@drawable/actionbar_tab_indicator</item>
</style>

同时确保您在AndroidManifest.xml

中使用主题
<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/ActionBarTheme" >

我注意到,对于旧版Android,如果主题不是AppCompat的子代,应用程序会崩溃。这应该是一个解决方法。

编辑:您还忘记在android:background之前放置icon等。

答案 2 :(得分:0)

将此内容写入您的样式xml文件中。

<item name="actionBarTabStyle">@style/CustomActionBarTabs </item>

<style name="CustomActionBarTabs"parent="@style/Widget.AppCompat.ActionBar.TabView">
<item name="android:background">@drawable/tab_indicator_ab_custom_actionbar_style</item>
<item name="android:textColor">#666666</item>
</style>

答案 3 :(得分:-1)

确保您已在

清单文件中应用app-theme

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/MyTheme"
    android:uiOptions="splitActionBarWhenNarrow"  >