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



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

<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>
...

但它并没有改变选项卡栏的样式。

我检查了我的代码很多次,我搜索并查看了我所有的笔记,但我找不到问题所在。有人知道答案吗?

问题出现在代码的这一部分:

<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>

这一行不应该在我想用作动作栏主题的自定义them中。正确的方法是将其添加到我的自定义应用程序下,所以我从上面的部分删除了这一行,并将其添加在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>

现在代码运行正常。

将其写入您的样式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>

试试这个:

<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:放在backgroundicon等之前。

确保已在

清单文件中应用app-theme

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

最新更新