更改Android Actionbar字幕颜色

时间:2013-07-04 20:18:43

标签: android actionbarsherlock android-theme

我希望自定义Android应用程序的actionBar 我正在使用Actionbar Sherlock 我的最小目标sdk是14,目标sdk 17.
我有一个主题,改变背景颜色和主标题文本
但是我不能改变字幕文字颜色
我的风格如下所示

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


什么做错了?

7 个答案:

答案 0 :(得分:21)

只需将titleTextStyle更改为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>

答案 1 :(得分:3)

这组样式格式化操作栏,包括背景,标题和副标题文本;它还格式化选项卡,包括所有状态(选定,未选中和所有聚焦/按下的组合),以及选项卡文本字体和大小(根据Android开发人员指令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>

当drawables是使用一些连贯的颜色值的渐变时(即未选择的薰衣草到紫色,未选择的紫色到淡紫色,将值更改为更暗的按压和更轻的焦点),这组样式看起来特别好。< / p>

答案 2 :(得分:2)

几乎就在那里。这是代码:

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

答案 3 :(得分:2)

在活动文件中添加以下代码

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

答案 4 :(得分:0)

我最终得到了它(它更接近我需要的)

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

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

答案 5 :(得分:0)

示例添加

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

答案 6 :(得分:0)

步骤:

1)按ID包含操作栏

2)检查操作栏是否存在(不为空)

3)检查apk版本

4)更改字幕的文本颜色和背景颜色

Toolbar actionBarToolbar = (Toolbar) findViewById(R.id.action_bar); //change R.id.action_bar with your action bar id

       if (actionBarToolbar != null) // check if action bar is not null

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // check version of apk

         actionBarToolbar.setBackgroundColor(getResources().getColor(R.color.colorPrimary)); // change actionBarToolbar background color

         actionBarToolbar.setSubtitleTextColor(Color.WHITE); // finally  this line change subtitle text color

        }