【问题标题】:Change the title text color in the Android ActionBar via xml通过xml更改Android ActionBar中的标题文本颜色
【发布时间】:2020-04-28 01:16:38
【问题描述】:

我想在我的应用中更改 ActionBar 的标题文本颜色。我已经尝试了很多方法,但我无法实现。我不想以编程方式执行此操作,因为当应用程序启动时,它会以以前的颜色显示操作栏,然后更改为新颜色。我从 Api Level 8 开始支持,我的 xml 是使用Android Action Bar Style Generator 生成的。然后我尝试用 this 更改标题文本颜色。标题文本颜色仍然是黑色。

这是我的代码:

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

【问题讨论】:

  • 你是指Action Bar中App名称的颜色吗?

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


【解决方案1】:

我解决了我的问题。问题是我正在使用 api 级别 14+ 的设备进行测试,而我只是将样式添加到 res/values 。我也必须在 res/values-v14 中添加样式。我附上我的代码:

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

    你必须做两件事:

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

    颜色.xml

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

    这个link 可能会帮助您选择合适的颜色。 这个link 也很有帮助。

    【讨论】:

    • 这是我的代码中的内容,它对我不起作用@SeahawksRdaBest
    • 错误是什么?你有 color.xml 文件 res>values>color.xml 吗?
    • 我的 styles.xml 中有颜色。没有错误,ActionBar 中的标题文本颜色不会更改为我放入文件中的颜色。 @SeahawksRdaBest
    • 完美运行!但您的最低 API 应设置为 13 或更高。
    【解决方案3】:

    也设置 titletextstyle 和 subtitletextstyle ... 这对我有用。 ..

    如果您想使用 xml 更改它... 这对我有用......

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

    【讨论】:

      【解决方案4】:

      对我来说,这个很有效。

      1. 转到 /value/colors.xml
      2. &lt;color name="black"&gt;#000000&lt;/color&gt; //if you want black 添加到您的&lt;resources&gt;
      3. 到 /values/styles.xml
      4. &lt;item name="titleTextColor"&gt;@color/black&lt;/item&gt; 添加到您的&lt;style&gt;

      这里还有一些示例代码: 颜色.xml:

      <resources>
          <color name="colorPrimary">#ffffff</color>
          <color name="colorPrimaryDark">#000000</color>
          <color name="colorAccent">#03DAC5</color>
          <color name="black">#000000</color>
      </resources>
      

      styles.xml:

          <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
              <!-- Customize your theme here. -->
              <item name="colorPrimary">@color/colorPrimary</item>
              <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
              <item name="colorAccent">@color/colorAccent</item>
              <item name="titleTextColor">@color/black</item>
          </style>```
      
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多