Change the title text color in the Android ActionBar via xml

♀尐吖头ヾ 提交于 2020-01-11 11:37:40

问题


I want to change the title text color of the ActionBar in my app. I have tried in many ways but I can't achieve it. I don't want to do it programatically because when the app is launched it shows the action bar with previous color and then changes to the new one. I'm supporting from Api Level 8 and my xml was generated with the Android Action Bar Style Generator. Then I tried to change the title text color with this . The title text color is still black.

This is my code:

<resources>

<style name="Theme.Dbtools_style" parent="@style/Theme.AppCompat.Light">
    <item name="actionBarItemBackground">@drawable/selectable_background_dbtools_style</item>
    <item name="popupMenuStyle">@style/PopupMenu.Dbtools_style</item>
    <item name="dropDownListViewStyle">@style/DropDownListView.Dbtools_style</item>
    <item name="actionBarTabStyle">@style/ActionBarTabStyle.Dbtools_style</item>
    <item name="actionDropDownStyle">@style/DropDownNav.Dbtools_style</item>
    <item name="actionBarStyle">@style/ActionBar.Solid.Dbtools_style</item>
    <item name="actionModeBackground">@drawable/cab_background_top_dbtools_style</item>
    <item name="actionModeSplitBackground">@drawable/cab_background_bottom_dbtools_style</item>
    <item name="actionModeCloseButtonStyle">@style/ActionButton.CloseMode.Dbtools_style</item>
    <!-- Remove icon in Action Bar -->
    <item name="android:displayOptions">showHome|homeAsUp|showTitle</item>
    <item name="displayOptions">showHome|homeAsUp|showTitle</item>
    <item name="android:icon">@android:color/transparent</item>
    <!-- Support library compatibility -->
    <item name="actionBarStyle">@style/ActionBar.Solid.Dbtools_style</item>
</style>

<style name="ActionBar.Solid.Dbtools_style" parent="@style/Widget.AppCompat.Light.ActionBar.Solid">
    <item name="background">@drawable/ab_solid_dbtools_style</item>
    <item name="backgroundStacked">@drawable/ab_stacked_solid_dbtools_style</item>
    <item name="backgroundSplit">@drawable/ab_bottom_solid_dbtools_style</item>
    <item name="progressBarStyle">@style/ProgressBar.Dbtools_style</item>
    <!-- Title Text Color -->
    <item name="android:titleTextStyle">@style/MyActionBarTitleText</item>
    <!-- Support library compatibility -->
    <item name="titleTextStyle">@style/MyActionBarTitleText</item>
</style>

<style name="ActionBar.Transparent.Dbtools_style" parent="@style/Widget.AppCompat.Light.ActionBar">
    <item name="background">@drawable/ab_transparent_dbtools_style</item>
    <item name="progressBarStyle">@style/ProgressBar.Dbtools_style</item>
</style>

<style name="PopupMenu.Dbtools_style" parent="@style/Widget.AppCompat.Light.PopupMenu">
    <item name="android:popupBackground">@drawable/menu_dropdown_panel_dbtools_style</item>
</style>

<style name="DropDownListView.Dbtools_style" parent="@style/Widget.AppCompat.Light.ListView.DropDown">
    <item name="android:listSelector">@drawable/selectable_background_dbtools_style</item>
</style>

<style name="ActionBarTabStyle.Dbtools_style" parent="@style/Widget.AppCompat.Light.ActionBar.TabView">
    <item name="android:background">@drawable/tab_indicator_ab_dbtools_style</item>
</style>

<style name="DropDownNav.Dbtools_style" parent="@style/Widget.AppCompat.Light.Spinner.DropDown.ActionBar">
    <item name="android:background">@drawable/spinner_background_ab_dbtools_style</item>
    <item name="android:popupBackground">@drawable/menu_dropdown_panel_dbtools_style</item>
    <item name="android:dropDownSelector">@drawable/selectable_background_dbtools_style</item>
</style>

<style name="ProgressBar.Dbtools_style" parent="@style/Widget.AppCompat.ProgressBar.Horizontal">
    <item name="android:progressDrawable">@drawable/progress_horizontal_dbtools_style</item>
</style>

<style name="ActionButton.CloseMode.Dbtools_style" parent="@style/Widget.AppCompat.Light.ActionButton.CloseMode">
    <item name="android:background">@drawable/btn_cab_done_dbtools_style</item>
</style>

<!-- this style is only referenced in a Light.DarkActionBar based theme -->
<style name="Theme.Dbtools_style.Widget" parent="@style/Theme.AppCompat">
    <item name="popupMenuStyle">@style/PopupMenu.Dbtools_style</item>
    <item name="dropDownListViewStyle">@style/DropDownListView.Dbtools_style</item>
</style>

<!-- ActionBar title text -->
<style name="MyActionBarTitleText" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">@color/white</item>
</style>


回答1:


I solved my problem. The problem was that I was testing in a device with api level 14+ and I just added the style to res/values . I had to add the style in res/values-v14 too. I attach my code:

res/values/styles.xml

<resources xmlns:android="http://schemas.android.com/apk/res/android">

<style name="Theme.Dbtools_style" parent="@style/Theme.AppCompat.Light">
    <!-- Title Text Color -->
    <item name="actionMenuTextColor">@color/white</item>
</style>

<style name="ActionBar.Solid.Dbtools_style" parent="@style/Widget.AppCompat.Light.ActionBar.Solid">
    <!-- Title Text Color -->
    <item name="titleTextStyle">@style/MyActionBarTitleText</item>
</style>

<!-- ActionBar title text -->
<style name="MyActionBarTitleText" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">@color/white</item>
</style>

res/values-v14/styles.xml

<style name="Theme.Dbtools_style" parent="@style/Theme.AppCompat.Light">

    <item name="android:actionBarStyle">@style/ActionBar.Solid.Dbtools_style</item>

    <!-- Title Text Color -->
    <item name="android:actionMenuTextColor">@color/white</item>
</style>

<style name="ActionBar.Solid.Dbtools_style" parent="@style/Widget.AppCompat.Light.ActionBar.Solid">
    <!-- Title Text color -->
    <item name="android:titleTextStyle">@style/MyActionBarTitleText</item>
</style>

<!-- ActionBar title text -->
<style name="MyActionBarTitleText" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">@color/white</item>
    <!-- The textColor property is backward compatible with the Support Library -->
</style>




回答2:


You Have to do two things:

Styles.xml

<style name="MyTheme" parent="@android:style/Theme.Holo">
         <item name="android:actionBarStyle">@style/MyActionBar</item>

</style>
<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
         <item name="android:background">#FF9D21</item>
         <item name="android:titleTextStyle">@style/MyActionBarTitleText</item>
</style> 
<style name="MyActionBarTitleText"
           parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
        <item name="android:textColor">@color/red</item>
</style>

Colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="red">#FF0000</color>
</resources>

This link might help you out with the appropriate color you want. This link is also helpful.




回答3:


Set titletextstyle and subtitletextstyle too ... This is what worked for me. ..

If you want to change that using xml ... This is what worked for me ...

<style name="MyActionBar"
    parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="android:background">@color/titleBarBgColor</item>
    <item name="android:titleTextStyle">@style/EldTheme.ActionBar.TitleTextStyle</item>
    <item name="android:subtitleTextStyle">@style/EldTheme.ActionBar.TitleTextStyle</item>
    <!-- Support library compatibility -->
    <item name="background">@color/titleBarBgColor</item>
    <item name="titleTextStyle">@style/EldTheme.ActionBar.TitleTextStyle</item>
    <item name="subtitleTextStyle">@style/EldTheme.ActionBar.TitleTextStyle</item>
</style>
<style name="EldTheme.ActionBar.TitleTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">@color/titleBarTextColor</item>
</style>


来源:https://stackoverflow.com/questions/24073004/change-the-title-text-color-in-the-android-actionbar-via-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!