Android: Title Bar Color don't change

2019-02-19 02:17发布

问题:

I wanna change my Apps Titlebar color and tried it in this way:

Part of the manifest file:

<application
        android:icon="@drawable/icon"
        android:label="@string/app_name"  
        android:theme="@style/Theme.MyTitleBar" ></application>

The style block part of the file styles.xml:

 <style name="Theme.MyTitleBar" parent="android:Theme">
        <item name="android:colorBackground">#FFFFFF</item>
        <item name="android:background">#FFFFFF</item>
        <item name="android:textColor">@android:color/black</item>
        <item name="android:textStyle">bold</item>
        <item name="android:textSize">16sp</item>
 </style>

But only the text color and attributes changes correct. Any guest what I did wrong?

回答1:

EDIT: I added the part for the ActionBar (sorry I put only for Dialogs at first)

You can follow this, it will allow you to change moreover other attributes that just the title bar:

Just call your custom theme in the manifest file:

 <application
    android:icon="@drawable/icon"
    android:label="@string/app_name"  
    android:theme="@style/AppTheme" >

Then create these 2 styles in your styles.xml. The first one defines the theme, the second one, one of the styles to apply on the theme:

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

<style name="ActionBarStyle" parent="android:style/Widget.Holo.Light.ActionBar">     
    <item name="android:colorBackground">#FFFFFF</item>
    <item name="android:background">#FFFFFF</item>
    <item name="android:textColor">@android:color/black</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textSize">16sp</item>
</style>

NOTE: In this example, I customize the theme Holo Light as you can see with this: android:style/Widget.Holo.Light.ActionBar.



回答2:

Add This style.xml File in your project according to your requirment...

    <item name="android:actionBarStyle">@style/myActionBar</item>

</style>



<style name="myActionBar" parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">    
    <item name="android:titleTextStyle">@style/myActionBar.titleTextStyle</item>
</style>

<style name="myActionBar.titleTextStyle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title.Inverse">
    <item name="android:textColor">@color/darkgrey</item>
</style>

 <style name="myActionBar" parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
    <item name="android:background">#e6ebc3</item>
</style>