Change Android Actionbar subtitle colour

2019-04-04 01:12发布

问题:

I wish to customize the actionBar of my android application.
I am using Actionbar Sherlock.
My min target sdk is 14, target sdk 17.
I have a theme that changes the background colour and main title text
however i cannot change the subtitle text colour
my style is shown here

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

<style name="MyTheme.ActionBarStyle" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
    <item name="android:background">#8B0000</item>
</style>

<style name="MyTheme.ActionBar.TitleTextStyle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:textColor">#FFFFFF</item>

</style>

<style name="MyTheme.ActionBar.Subtitle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Subtitle">
    <item name="android:textColor">#FF0000</item>

</style>


what have a done wrong?

回答1:

Just change titleTextStyle to subtitleTextStyle

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

  <style name="MyTheme.ActionBarStyle"parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:subtitleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
  </style>

  <style name="MyTheme.ActionBar.TitleTextStyle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:textColor">@color/red</item>
  </style>


回答2:

This set of styles formats the action bar, including background, title and subtitle text; it also formats the tabs, including all states (selected, unselected, and all the focused/pressed combinations), as well as the tab text font and size (per Android developers instruction https://developer.android.com/training/basics/actionbar/styling.html):

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/themeActionBar</item>
    <item name="android:actionBarTabTextStyle">@style/styleActionBarTabText</item>
    <item name="android:actionMenuTextColor">@color/actionbar_text</item>
    <item name="android:actionBarTabStyle">@style/styleActionBarTabs</item>
    <!-- Support library compatibility -->
    <item name="actionBarStyle">@style/themeActionBar</item>
    <item name="actionBarTabTextStyle">@style/styleActionBarTabText</item>
    <item name="actionMenuTextColor">@color/actionbar_text</item>
    <item name="actionBarTabStyle">@style/styleActionBarTabs</item>
</style>

<!-- ActionBar styles -->
<style name="themeActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="android:background">@drawable/abactionbargradient</item>
    <item name="android:titleTextStyle">@style/styleActionBarTitleText</item>
    <item name="android:subtitleTextStyle">@style/styleActionBarSubTitleText</item>
    <!-- Support library compatibility -->
    <item name="background">@drawable/abactionbargradient</item>
    <item name="titleTextStyle">@style/styleActionBarTitleText</item>
    <item name="subtitleTextStyle">@style/styleActionBarSubTitleText</item>
</style>

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

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

<!-- ActionBar tabs text -->
<style name="styleActionBarTabText"
    parent="@style/Widget.AppCompat.ActionBar.TabText">
    <item name="android:textColor">@color/actionbar_text</item>
    <item name="android:textSize">10dp</item>
    <!-- The textColor property is backward compatible with the Support Library -->
</style>

<!-- ActionBar tabs styles -->
<style name="styleActionBarTabs"
    parent="@style/Widget.AppCompat.ActionBar.TabView">
    <!-- tab indicator -->
    <item name="android:background">@drawable/abactionbartabs</item>

    <!-- Support library compatibility -->
    <item name="background">@drawable/abactionbartabs</item>
</style>

This set of styles looks especially nice when the drawables are gradients using some set of coherent color values (i.e. lavender to purple for selected and purple to lavender for unselected, changing the values to darker for pressed and lighter for focused).



回答3:

Almost there. Here is the code:

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

<style name="MyTheme.ActionBarStyle" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
    <item name="android:subtitleTextStyle">@style/MyTheme.ActionBar.Subtitle</item>
    <item name="android:background">#8B0000</item>
</style>

<style name="MyTheme.ActionBar.TitleTextStyle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:textColor">#FFFFFF</item>
</style>

<style name="MyTheme.ActionBar.Subtitle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Subtitle">
    <item name="android:textColor">#FF0000</item>
</style>


回答4:

Add below code in your activity file

 actionBar.setSubtitle(Html.fromHtml("<font color='#FFBF00'>Here ActionBar Subtitle</font>"));


回答5:

I ended up with this (Its closer to what i need)

@style/Widget.Styled.ActionBar @style/Widget.Styled.ActionBar

<style name="Widget.Styled.ActionBar" parent="Widget.Sherlock.ActionBar.Solid">
    <item name="subtitleTextStyle">@style/TextAppearance.Sherlock.Widget.Styled.ActionBar.Subtitle</item>
    <item name="android:subtitleTextStyle">@style/TextAppearance.Sherlock.Widget.Styled.ActionBar.Subtitle</item>
    <item name="android:background">#8B0000</item>
</style>

<style name="TextAppearance.Sherlock.Widget.Styled.ActionBar.Subtitle" parent="Widget">
    <item name="android:textSize">12sp</item>
</style>

<style name="OverFlow" parent="Widget.Sherlock.ActionButton.Overflow">
    <item name="android:src">@drawable/abs__ic_menu_moreoverflow_holo_light</item>
</style>




回答6:

example add

<android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            android:background="@color/colorPrimary"
            app:titleTextColor="@color/white"
            app:subtitleTextColor="@color/white"
            android:gravity="center"
            >

subtitleColor

        app:subtitleTextColor="@color/white"