简体   繁体   中英

How to change the title text color and the color of the background on the ActionBar?

Here's my styles.xml file:

<resources>

    <!--
        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="android:Theme.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. -->
    </style>

    <style name="MyCustomTheme" parent="@android:style/Theme.Holo.Light">
        <item name="android:actionBarStyle">@style/MyActionBarTheme</item>
        <item name="android:actionMenuTextColor">@style/TitleBarTextColor</item>
    </style>

    <style name="MyActionBarTheme" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">#060EBB</item>
    </style>

    <style name="MyActionBarTitleText"
           parent="@style/TextAppearance.Holo.Widget.ActionBar.Title">
        <item name="android:textColor">#00B300</item>
    </style>

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



</resources>

Here's my manifest file:

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

The background of the ActionBar is changing color the way I want it to, but the text itself isn't. What am I doing wrong? Oh, and some explanation of the way styles.xml works within Android would be nice too. It seems to be very confusing.

change your code from

    <style name="MyActionBarTheme" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:background">#060EBB</item>
    </style>

to

    <style name="MyActionBarTheme" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:background">#060EBB</item>
    <item name="android:titleTextStyle">@style/TitleColor</item>
    </style>

<style name="TitleColor" parent="android:TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:textColor">#00B300</item>
</style>

Style.xml

style.xml file states what are the styles you want to use in your app, basically it inherits android default style as you can see in every style tag which ever property you want to set you give it a name

<style name="TitleColor" parent="android:TextAppearance.Holo.Widget.ActionBar.Title">

and say what property of parent style you want to override, Parent style is the theme which you selected as default theme when you created app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM