操作栏文本颜色

发布于 2024-11-04 19:23:16 字数 203 浏览 6 评论 0原文

如何更改 ActionBar 的文本颜色?我继承了 Holo Light 主题,我可以更改 ActionBar 的背景,但我不知道要调整什么属性来更改文本颜色。


好的,我可以使用属性 android:textColorPrimary 更改文本颜色,但它也会更改 ActionBar 按钮上发生溢出时显示的下拉菜单的文本颜色。知道如何更改这些下拉菜单/列表的颜色吗?

how can I change the text color of the ActionBar? I've inherited the Holo Light Theme, I'm able to change the background of the ActionBar but I don't find out what is the attribute to tweak to change the text color.


Ok, I'm able to change the text color with the attribute android:textColorPrimary but it also changes the text color of the dropdown menu displayed when an overflow happen on the ActionBar buttons. Any idea how to change the color of those dropdown menu / List ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(28

乞讨 2024-11-11 19:23:16

好吧,我找到了更好的方法。我现在只能更改标题的颜色。您还可以调整字幕。

这是我的 styles.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <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>
  </style>

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

Ok, I've found a better way. I'm now able to only change the color of the title. You can also tweak the subtitle.

Here is my styles.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <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>
  </style>

  <style name="MyTheme.ActionBar.TitleTextStyle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:textColor">@color/red</item>
  </style>
</resources>
简单 2024-11-11 19:23:16

我找到了一种适用于任何风格的 ActionBarSherlockCompatNative)的方法

:使用html设置标题,并指定文本颜色。例如,要将 ActionBar 文本颜色设置为红色,只需执行以下操作:

getActionBar()/* or getSupportActionBar() */.setTitle(Html.fromHtml("<font color=\"red\">" + getString(R.string.app_name) + "</font>"));

您还可以使用红色十六进制代码 #FF0000 代替单词 red.如果您遇到问题,请参阅 Android Html。 fromHtml(String) 不适用于 text


此外,如果您想使用颜色资源,可以使用此代码获取正确的十六进制字符串,并根据需要删除 alpha(font 标签不支持 alpha):

int orange = getResources().getColor(R.color.orange);
String htmlColor = String.format(Locale.US, "#%06X", (0xFFFFFF & Color.argb(0, Color.red(orange), Color.green(orange), Color.blue(orange))));

I found a way that works well with any flavor of ActionBar (Sherlock, Compat, and Native):

Just use html to set the title, and specify the text color. For example, to set the ActionBar text color to red, simply do this:

getActionBar()/* or getSupportActionBar() */.setTitle(Html.fromHtml("<font color=\"red\">" + getString(R.string.app_name) + "</font>"));

You can also use the red hex code #FF0000 instead of the word red. If you are having trouble with this, see Android Html.fromHtml(String) doesn't work for <font color='#'>text</font>.


Additionally, if you want to use a color resource, this code can be used to get the correct HEX String, and removing the alpha if needed (the font tag does not support alpha):

int orange = getResources().getColor(R.color.orange);
String htmlColor = String.format(Locale.US, "#%06X", (0xFFFFFF & Color.argb(0, Color.red(orange), Color.green(orange), Color.blue(orange))));
花海 2024-11-11 19:23:16

我遇到了和你一样的问题,它是一个 Holo.Light 主题,但我想设置 ActionBar 颜色的样式,所以我还需要更改文本颜色以及标题和菜单。所以最后我去了 git hub 并查看了源代码,直到找到该死的正确样式:

<?xml version="1.0" encoding="utf-8"?>
<!-- For honeycomb and up -->
<resources>

    <style name="myTheme" parent="@android:style/Theme.Holo.Light">
        <item name="android:actionBarStyle">@style/myTheme.ActionBar</item>
        <item name="android:actionMenuTextColor">@color/actionBarText</item>
    </style>

    <style name="myTheme.ActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">@drawable/actionbarbground</item>
        <item name="android:titleTextStyle">@style/myTheme.ActionBar.Text</item>
    </style>

    <style name="myTheme.ActionBar.Text" parent="@android:style/TextAppearance">
        <item name="android:textColor">@color/actionBarText</item>
    </style>

</resources>

所以现在你所要做的就是设置任何 @color/actionBarText@drawable/你想要的actionbarbground

I was having the same problem as you, it's a Holo.Light theme but I wanted to style the ActionBar color, so I needed to change the text color as well and also both Title and Menus. So at the end I went to git hub and looked at source code until I find the damn correct style:

<?xml version="1.0" encoding="utf-8"?>
<!-- For honeycomb and up -->
<resources>

    <style name="myTheme" parent="@android:style/Theme.Holo.Light">
        <item name="android:actionBarStyle">@style/myTheme.ActionBar</item>
        <item name="android:actionMenuTextColor">@color/actionBarText</item>
    </style>

    <style name="myTheme.ActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">@drawable/actionbarbground</item>
        <item name="android:titleTextStyle">@style/myTheme.ActionBar.Text</item>
    </style>

    <style name="myTheme.ActionBar.Text" parent="@android:style/TextAppearance">
        <item name="android:textColor">@color/actionBarText</item>
    </style>

</resources>

so now all you have to do is set whatever @color/actionBarText and@drawable/actionbarbground you want!

樱&纷飞 2024-11-11 19:23:16

ActionBar ID 不能直接获得,因此您必须在这里进行一些修改。

int actionBarTitleId = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
if (actionBarTitleId > 0) {
    TextView title = (TextView) findViewById(actionBarTitleId);
    if (title != null) {
        title.setTextColor(Color.RED);
    }
}

The ActionBar ID is not available directly, so you have to do little bit of hacking here.

int actionBarTitleId = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
if (actionBarTitleId > 0) {
    TextView title = (TextView) findViewById(actionBarTitleId);
    if (title != null) {
        title.setTextColor(Color.RED);
    }
}
佼人 2024-11-11 19:23:16

最直接的方法是在 styles.xml 中执行此操作。

Google 的模板 styles.xml 当前会生成以下内容:

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

如果您在结束标记之前再添加一行,如图所示,这会将文本颜色更改为深色 ActionBar 应有的颜色:

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="actionBarTheme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
</style>

如果您想将颜色自定义为其他颜色,您可以在colors.xml中指定您自己的颜色,甚至可以使用android:textColorPrimary属性使用Android的内置颜色:

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


<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="android:textColorPrimary">@android:color/darker_gray</item>
</style>

注意:这会更改标题的颜色以及ActionBar中显示的任何菜单项的标题。

The most straight-forward way is to do this in the styles.xml.

Google's template styles.xml currently generates the following:

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

If you add one more line before the closing tag, as shown, that will change the text color to what it should be with a Dark ActionBar:

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="actionBarTheme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
</style>

If you want to customize the color to something else, you can either specify your own color in colors.xml or even use a built-in color from Android using the android:textColorPrimary attribute:

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


<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="android:textColorPrimary">@android:color/darker_gray</item>
</style>

Note: This changes the color of the title and also the titles of any MenuItems displayed in the ActionBar.

樱花落人离去 2024-11-11 19:23:16

这是一个老话题,但对于未来的读者来说,使用 ToolBar 使一切变得非常简单:

Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
toolbar.setTitle(R.string.app_name);
toolbar.setTitleTextColor(getResources().getColor(R.color.someColor));
setSupportActionBar(toolbar);

It's an old topic but for future readers, using ToolBar makes everything very easy:

Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
toolbar.setTitle(R.string.app_name);
toolbar.setTitleTextColor(getResources().getColor(R.color.someColor));
setSupportActionBar(toolbar);
久夏青 2024-11-11 19:23:16

我已经用简单的一行代码完成了

actionBar.setTitle(Html.fromHtml("<font color='#ff0000'>ActionBartitle </font>"));

i have done with simple one line code

actionBar.setTitle(Html.fromHtml("<font color='#ff0000'>ActionBartitle </font>"));
无敌元气妹 2024-11-11 19:23:16

将其添加到操作栏的根部。我遇到了这个问题。

<style name="ActionBar" parent="@style/Theme.AppCompat.Light">
    <item name="actionBarStyle">@style/Widget.Styled.ActionBar</item>
    <item name="actionMenuTextColor">@color/stdDarkBlueText</item>
</style>

<style name="Widget.Styled.ActionBar" parent="@style/Widget.AppCompat.Light.ActionBar">
    <item name="titleTextStyle">@style/ActionBarTitleText</item>
    <item name="subtitleTextStyle">@style/ActionBarSubTitleText</item>
</style>

<style name="ActionBarTitleText" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">@color/stdDarkBlueText</item>
    <item name="android:textSize">12sp</item>
</style>

<style name="ActionBarSubTitleText" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Subtitle">
    <item name="android:textColor">@color/stdDarkBlueText</item>
    <item name="android:textSize">12sp</item>
</style>

actionMenuTextColor - 更改操作栏文本的文本颜色,当它是 Widget.Styled.ActionBar 的一部分时,它从未起作用,因此我必须将其添加到根目录中。其他 2 个属性更改操作栏的标题和副标题。

Add it to the root of the action bar. I had this problem.

<style name="ActionBar" parent="@style/Theme.AppCompat.Light">
    <item name="actionBarStyle">@style/Widget.Styled.ActionBar</item>
    <item name="actionMenuTextColor">@color/stdDarkBlueText</item>
</style>

<style name="Widget.Styled.ActionBar" parent="@style/Widget.AppCompat.Light.ActionBar">
    <item name="titleTextStyle">@style/ActionBarTitleText</item>
    <item name="subtitleTextStyle">@style/ActionBarSubTitleText</item>
</style>

<style name="ActionBarTitleText" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">@color/stdDarkBlueText</item>
    <item name="android:textSize">12sp</item>
</style>

<style name="ActionBarSubTitleText" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Subtitle">
    <item name="android:textColor">@color/stdDarkBlueText</item>
    <item name="android:textSize">12sp</item>
</style>

actionMenuTextColor - changes text color for the action bar text, it never worked when it was part of the Widget.Styled.ActionBar, so I had to add it to the root. Other 2 attributes change title and subtitle of an action bar.

雨后彩虹 2024-11-11 19:23:16

在操作栏上设置 HTML 字符串不适用于 SDK v21+ 中的 Material 主题。

如果您想更改它,您应该在 style.xml 中设置主要文本颜色

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="android:Theme.Material.Light">
        <!-- Customize your theme here. -->
        <item name="android:textColorPrimary">@color/actionbar-text-color</item>
    </style>
</resources>    

Setting a HTML string on the action bar doesn't work on the Material theme in SDK v21+

If you want to change it you should set the primary text color in your style.xml

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="android:Theme.Material.Light">
        <!-- Customize your theme here. -->
        <item name="android:textColorPrimary">@color/actionbar-text-color</item>
    </style>
</resources>    
月隐月明月朦胧 2024-11-11 19:23:16

如果您还想设置副标题的样式,只需将其添加到您的自定义样式中即可。

<item name="android:subtitleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>

如果有人希望获得 AppCompat 库相同的结果,那么这就是我使用的:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomActivityTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
        <item name="actionBarStyle">@style/MyActionBar</item>
        <!-- other activity and action bar styles here -->
    </style>

    <!-- style for the action bar backgrounds -->
    <style name="MyActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:background">@drawable/actionbar_background</item>
        <item name="background">@drawable/actionbar_background</item>
        <item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
        <item name="android:subtitleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
        <item name="titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
        <item name="subtitleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
     </style>
    <style name="MyTheme.ActionBar.TitleTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
        <item name="android:textColor">@color/color_title</item>
      </style>
</resources>

If you want to style the subtitle also then simply add this in your custom style.

<item name="android:subtitleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>

People who are looking to get the same result for AppCompat library then this is what I used:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomActivityTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
        <item name="actionBarStyle">@style/MyActionBar</item>
        <!-- other activity and action bar styles here -->
    </style>

    <!-- style for the action bar backgrounds -->
    <style name="MyActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:background">@drawable/actionbar_background</item>
        <item name="background">@drawable/actionbar_background</item>
        <item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
        <item name="android:subtitleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
        <item name="titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
        <item name="subtitleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
     </style>
    <style name="MyTheme.ActionBar.TitleTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
        <item name="android:textColor">@color/color_title</item>
      </style>
</resources>
近箐 2024-11-11 19:23:16

许多答案已被弃用,因此我将更新线程并展示如何更改 ActionBar(工具栏)和 ActionBar 弹出菜单中的文本颜色和背景颜色。

Android 中使用操作栏(工具栏)的最新方法(截至 2018 年 5 月)是将主题与 NoActionBar 一起使用,并使用工具栏。

所以这就是您所需要的:

  1. 在 Activity(扩展 AppCompatActivity)中声明 Toolbar:

    工具栏 myToolbar = (工具栏) findViewById(R.id.toolbar);
    setSupportActionBar(myToolbar);
    
  2. 在 Activity 的布局 xml 中添加 Toolbar。在这里,我们将设置自定义(覆盖主题),请注意 android:theme="@style/ThemeOverlay.AppTheme.ActionBar"app:popupTheme="@style/ThemeOverlay.AppTheme.PopupMenu “,他们会成功的。

    
    
  3. 在 styles.xml 中,您必须覆盖这两种样式才能设置自定义颜色:

    <样式名称=“AppTheme”parent=“Theme.AppCompat.Light.NoActionBar”>
    
    <项目名称=“colorPrimary”>@color/colorPrimary
    <项目名称=“colorPrimaryDark”>@color/colorPrimaryDark
    <项目名称=“colorAccent”>@color/colorAccent
    <项目名称=“actionBarTheme”>@style/ThemeOverlay.AppTheme.ActionBar
    @style/ThemeOverlay.AppTheme.PopupMenu;
    
    
    <样式名称=“ThemeOverlay.AppTheme.ActionBar”parent=“ThemeOverlay.AppCompat.Dark.ActionBar”>
    <项目名称=“android:textColorPrimary”>@color/action_bar_text_color
    <项目名称=“android:background”>@color/action_bar_bg_color
    
    
    <样式名称=“ThemeOverlay.AppTheme.PopupMenu”parent=“ThemeOverlay.AppCompat.Dark.ActionBar”>
    <项目名称=“android:background”>@color/popup_bg_color
    <项目名称=“android:textColorPrimary”>@color/popup_text_color
    
    

就是这样

。如果你问我,我会说:是的,整个主题的事情一团糟。

A lot of answers are deprecated so I'll update the thread and show how to change text color and backgroundcolor on ActionBar (Toolbar) and in ActionBar pop up menu.

The latest approach in Android (as of May 2018) in working with Action bar (Toolbar) is to use Theme with NoActionBar and use Toolbar instead.

so here is what you need:

  1. In Activity (which extends AppCompatActivity) declare Toolbar:

    Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(myToolbar);
    
  2. Add Toolbar in the Activity's layout xml. Here we will set our custom (overwritten themes), note android:theme="@style/ThemeOverlay.AppTheme.ActionBar" and app:popupTheme="@style/ThemeOverlay.AppTheme.PopupMenu", they will do the trick.

    <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppTheme.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppTheme.PopupMenu"
    app:layout_constraintTop_toTopOf="parent" />
    
  3. In your styles.xml you will have to overwrite those two styles to set custom colors:

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="actionBarTheme">@style/ThemeOverlay.AppTheme.ActionBar</item>
    <item name="actionBarPopupTheme">@style/ThemeOverlay.AppTheme.PopupMenu</item>
    </style>
    
    <style name="ThemeOverlay.AppTheme.ActionBar" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="android:textColorPrimary">@color/action_bar_text_color</item>
    <item name="android:background">@color/action_bar_bg_color</item>
    </style>
    
    <style name="ThemeOverlay.AppTheme.PopupMenu" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="android:background">@color/popup_bg_color</item>
    <item name="android:textColorPrimary">@color/popup_text_color</item>
    </style>
    

That's it folks

p.s. if you ask me I would say: YES, this whole Theme thing is a hell of a mess.

爱的十字路口 2024-11-11 19:23:16

找到了一种很好的方法,无需在 API >= 21 上创建自己的布局。

它只会对操作栏中的文本进行着色并控制可绘制对象。

希望它对某人有用。

<!--Material design primary colors-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/accent</item>
    <item name="android:navigationBarColor">@color/primary_dark</item>
    <item name="actionBarTheme">@style/AppBaseTheme.Toolbar</item>
</style>

<!--Action bar-->
<style name="AppBaseTheme.Toolbar" parent="Widget.AppCompat.ActionBar.Solid">
    <item name="android:textColorPrimary">@color/action_bar_text</item>
    <item name="colorControlNormal">@color/action_bar_text</item>
</style>

Found the way to do it nicely without creating your own layout on API >= 21.

It will only colorize texts and control drawables inside the action bar.

Hope it will be useful for someone.

<!--Material design primary colors-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/accent</item>
    <item name="android:navigationBarColor">@color/primary_dark</item>
    <item name="actionBarTheme">@style/AppBaseTheme.Toolbar</item>
</style>

<!--Action bar-->
<style name="AppBaseTheme.Toolbar" parent="Widget.AppCompat.ActionBar.Solid">
    <item name="android:textColorPrimary">@color/action_bar_text</item>
    <item name="colorControlNormal">@color/action_bar_text</item>
</style>
灯下孤影 2024-11-11 19:23:16

运行良好

这些函数在 Java 中

private void setActionbarTextColor(ActionBar actBar, int color) {

    String title = actBar.getTitle().toString();
    Spannable spannablerTitle = new SpannableString(title);
    spannablerTitle.setSpan(new ForegroundColorSpan(color), 0, spannablerTitle.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    actBar.setTitle(spannablerTitle);

}

,然后使用它只需向其提供操作栏和新颜色,即

ActionBar actionBar = getActionBar();    // Or getSupportActionBar() if using appCompat
int red = Color.RED
setActionbarTextColor(actionBar, red);

在 Kotlin 中,

您可以使用如下扩展函数:

private fun ActionBar.setTitleColor(color: Int) {
    val text = SpannableString(title ?: "")
    text.setSpan(ForegroundColorSpan(color),0,text.length, Spannable.SPAN_INCLUSIVE_INCLUSIVE)
    title = text
}

然后将其应用于您的 ActionBar

actionBar?.setTitleColor(Color.RED)

These functions work well

In Java

private void setActionbarTextColor(ActionBar actBar, int color) {

    String title = actBar.getTitle().toString();
    Spannable spannablerTitle = new SpannableString(title);
    spannablerTitle.setSpan(new ForegroundColorSpan(color), 0, spannablerTitle.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    actBar.setTitle(spannablerTitle);

}

then to use it just feed it your action bar and the new color i.e.

ActionBar actionBar = getActionBar();    // Or getSupportActionBar() if using appCompat
int red = Color.RED
setActionbarTextColor(actionBar, red);

In Kotlin

You can use an extension function like this:

private fun ActionBar.setTitleColor(color: Int) {
    val text = SpannableString(title ?: "")
    text.setSpan(ForegroundColorSpan(color),0,text.length, Spannable.SPAN_INCLUSIVE_INCLUSIVE)
    title = text
}

And then apply to your ActionBar with

actionBar?.setTitleColor(Color.RED)
傲娇萝莉攻 2024-11-11 19:23:16

这是我检查所有答案后使用的解决方案

<!-- Base application theme. -->
<style name="AppTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorAccent">@color/Button_color</item>
    <item name="android:editTextStyle">@style/EditTextStyle</item>
    <item name="android:typeface">monospace</item>
    <item name="android:windowActionBar">true</item>
    <item name="colorPrimary">@color/Title_Bar_Color</item>
    <item name="android:actionBarStyle">@style/Widget.Styled.ActionBar</item>
    <item name="actionBarStyle">@style/Widget.Styled.ActionBar</item>

</style>

<style name="EditTextStyle" parent="Widget.AppCompat.EditText"/>

<style name="Widget.Styled.ActionBar" parent="@style/Widget.AppCompat.Light.ActionBar">
    <item name="titleTextStyle">@style/ActionBarTitleText</item>
    <item name="android:background">@color/Title_Bar_Color</item>
    <item name="background">@color/Title_Bar_Color</item>
    <item name="subtitleTextStyle">@style/ActionBarSubTitleText</item>
</style>

<style name="ActionBarTitleText" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">@color/solid_white</item>
    <item name="android:textSize">12sp</item>
</style>

<style name="ActionBarSubTitleText" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Subtitle">
    <item name="android:textColor">@color/solid_white</item>
    <item name="android:textSize">12sp</item>
</style>

更改所需的颜色即可工作

This is the solution I used after checking all answers

<!-- Base application theme. -->
<style name="AppTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorAccent">@color/Button_color</item>
    <item name="android:editTextStyle">@style/EditTextStyle</item>
    <item name="android:typeface">monospace</item>
    <item name="android:windowActionBar">true</item>
    <item name="colorPrimary">@color/Title_Bar_Color</item>
    <item name="android:actionBarStyle">@style/Widget.Styled.ActionBar</item>
    <item name="actionBarStyle">@style/Widget.Styled.ActionBar</item>

</style>

<style name="EditTextStyle" parent="Widget.AppCompat.EditText"/>

<style name="Widget.Styled.ActionBar" parent="@style/Widget.AppCompat.Light.ActionBar">
    <item name="titleTextStyle">@style/ActionBarTitleText</item>
    <item name="android:background">@color/Title_Bar_Color</item>
    <item name="background">@color/Title_Bar_Color</item>
    <item name="subtitleTextStyle">@style/ActionBarSubTitleText</item>
</style>

<style name="ActionBarTitleText" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">@color/solid_white</item>
    <item name="android:textSize">12sp</item>
</style>

<style name="ActionBarSubTitleText" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Subtitle">
    <item name="android:textColor">@color/solid_white</item>
    <item name="android:textSize">12sp</item>
</style>

Change the required colors it will work

ぽ尐不点ル 2024-11-11 19:23:16

尝试将其添加到您的 Activity 的 onCreate 中。适用于几乎所有 Android 版本。

 actionBar.setTitle(Html.fromHtml("<font color='#ffff00'>Your Title</font>"));  

或者

getSupportActionBar().setTitle(Html.fromHtml("<font color='#ffff00'>Your Title</font>"));

Try adding this in your Activity's onCreate. Works on almost every Android version.

 actionBar.setTitle(Html.fromHtml("<font color='#ffff00'>Your Title</font>"));  

or

getSupportActionBar().setTitle(Html.fromHtml("<font color='#ffff00'>Your Title</font>"));
反差帅 2024-11-11 19:23:16

只需将其放入您的主题风格即可。

@color/yourcolor

just put this in your thme style.

<item name="android:textColorPrimary">@color/yourcolor</item>

帅气尐潴 2024-11-11 19:23:16

尝试了很多方法,在低版本的API中,可行的方法是 @color/your_color 并且不要使用Android命名空间,希望可以帮助你

ps:

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

Try a lot of methods, in the low version of the API,a feasible method is <item name="actionMenuTextColor">@color/your_color</item> and don't use the Android namespace,hope can help you

ps:

  <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="actionMenuTextColor">@color/actionMenuTextColor</item>
</style>
傾城如夢未必闌珊 2024-11-11 19:23:16

最简单的方法是:
将这两行添加到您的 styles.xml 文件中,

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
<item name="android:textColorSecondary">@color/white</item>
<item name="android:textColor">@color/white</item>
</style>

并将颜色替换为您的颜色。

The most simple way is :
Add these two lines to your styles.xml file

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
<item name="android:textColorSecondary">@color/white</item>
<item name="android:textColor">@color/white</item>
</style>

Replace the color with your color.

浅忆 2024-11-11 19:23:16

这里的Style.xml就像

 <resources>
<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="actionBarTheme">@style/MyTheme</item>
</style>


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

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

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

添加

<item name="actionBarTheme">@style/MyTheme</item> 

你应该在AppTheme中

Here Style.xml is like

 <resources>
<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="actionBarTheme">@style/MyTheme</item>
</style>


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

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

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

You should add

<item name="actionBarTheme">@style/MyTheme</item> 

in AppTheme

忘年祭陌 2024-11-11 19:23:16

如果您需要以编程方式设置颜色,请执行以下操作:

static void setActionBarTextColor(Activity activity, int color)
{
      ActionBar actionBar = activity instanceof AppCompatActivity
                    ? ((AppCompatActivity) activity).getSupportActionBar()
                    : activity.getActionBar();
      String title = activity.getTitle(); // or any title you want
      SpannableString ss = new SpannableString(title);
      ss.setSpan(new ForegroundColorSpan(color), 0, title.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
      actionBar.setTitle(ss);
}

If you need to set the color programmatically this is the way to do it:

static void setActionBarTextColor(Activity activity, int color)
{
      ActionBar actionBar = activity instanceof AppCompatActivity
                    ? ((AppCompatActivity) activity).getSupportActionBar()
                    : activity.getActionBar();
      String title = activity.getTitle(); // or any title you want
      SpannableString ss = new SpannableString(title);
      ss.setSpan(new ForegroundColorSpan(color), 0, title.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
      actionBar.setTitle(ss);
}
楠木可依 2024-11-11 19:23:16

就我而言,我切换到 AndroidX Toolbar 而不是使用本机操作栏。另外,我依靠 视图绑定 从活动访问工具栏,但您可以使用 findViewById(...) 来实现。

(我删除了不相关的代码)。

styles.xml

...
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
  ...
</style>
...

activity_main.xml

...
<androidx.appcompat.widget.Toolbar
  android:id="@+id/toolbarMain"
  android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
  app:titleTextColor="@color/colorOnPrimary"
  ...
/>
...

MainActivity.kt

class MainActivity : AppCompatActivity() {

  private lateinit var binding: ActivityMainBinding
  ...

  override fun onCreate(savedInstanceState: Bundle?) {
    ...
    binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
    setSupportActionBar(binding.toolbarMain)
    ...
  }

  ...
}

In my case, I switched to AndroidX Toolbar instead of using the native action bar. Also, I rely on view binding to access the toolbar from the activity, but you can use findViewById(...) for that.

(I removed the code which is irrelevant).

styles.xml:

...
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
  ...
</style>
...

activity_main.xml:

...
<androidx.appcompat.widget.Toolbar
  android:id="@+id/toolbarMain"
  android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
  app:titleTextColor="@color/colorOnPrimary"
  ...
/>
...

MainActivity.kt:

class MainActivity : AppCompatActivity() {

  private lateinit var binding: ActivityMainBinding
  ...

  override fun onCreate(savedInstanceState: Bundle?) {
    ...
    binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
    setSupportActionBar(binding.toolbarMain)
    ...
  }

  ...
}
-柠檬树下少年和吉他 2024-11-11 19:23:16

一个不错的解决方案是使用 SpannableStringBuilder 并以这种方式设置颜色。您甚至可以在字符串的不同部分使用不同的颜色,添加图像等。

已使用新的支持库进行测试。

请参阅 Android:使用 TextView.setText() 对字符串的一部分进行着色?

A nice solution is to user a SpannableStringBuilder and set the color that way. You can even use different colors on different parts of the string, add images etc.

Tested with the new support library.

See Android: Coloring part of a string using TextView.setText()?

不寐倦长更 2024-11-11 19:23:16

这不是推荐的解决方案,因为我将在此处使用 android api,但由于我的应用程序需要在 xml 此处不可能的条件下动态更改主题,所以我需要这样做。但这个解决方案工作得非常好。

解决方案: -

 /**
 * 
 * @author Kailash Dabhi
 * @email [email protected]
 *
 */ 
 public static void setActionbarTextColor(Activity activity, int color) {
    Field mActionViewField;
    try {
        mActionViewField = activity.getActionBar().getClass()
                .getDeclaredField("mActionView");
        mActionViewField.setAccessible(true);
        Object mActionViewObj = mActionViewField.get(activity
                .getActionBar());

        Field mTitleViewField = mActionViewObj.getClass().getDeclaredField(
                "mTitleView");
        mTitleViewField.setAccessible(true);
        Object mTitleViewObj = mTitleViewField.get(mActionViewObj);

        TextView mActionBarTitle = (TextView) mTitleViewObj;
        mActionBarTitle.setTextColor(color);
        // Log.i("field", mActionViewObj.getClass().getName());
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    }

}

This is not the recommended solution as I am going in android apis here but as my application requires to change the theme dynmically on conditions xml not possible here, So I need to do this. But This solution is working very nice.

Solution:--

 /**
 * 
 * @author Kailash Dabhi
 * @email [email protected]
 *
 */ 
 public static void setActionbarTextColor(Activity activity, int color) {
    Field mActionViewField;
    try {
        mActionViewField = activity.getActionBar().getClass()
                .getDeclaredField("mActionView");
        mActionViewField.setAccessible(true);
        Object mActionViewObj = mActionViewField.get(activity
                .getActionBar());

        Field mTitleViewField = mActionViewObj.getClass().getDeclaredField(
                "mTitleView");
        mTitleViewField.setAccessible(true);
        Object mTitleViewObj = mTitleViewField.get(mActionViewObj);

        TextView mActionBarTitle = (TextView) mTitleViewObj;
        mActionBarTitle.setTextColor(color);
        // Log.i("field", mActionViewObj.getClass().getName());
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    }

}
方圜几里 2024-11-11 19:23:16

对于 Android 5(棒棒糖),您必须使用 android:actionBarPopupTheme 来设置溢出菜单的 textColor

For Android 5 (lollipop) you will have to use android:actionBarPopupTheme to set the textColor for the overflow menu.

青朷 2024-11-11 19:23:16

您可以直接在 xml 文件中使用 html 属性来更改任何文本的颜色。
例如在 strings.xml 中:

<resources>
    <string name = "app_name">
        <html><font color="#001aff">Multi</font></html>
        <html><font color="#ff0044">color</font></html>
        <html><font color="#e9c309">Text </font></html>
    </string>
</resources>

在此处输入图像描述

you can change color of any text by use html <font> attribute directly in xml files.
for example in strings.xml :

<resources>
    <string name = "app_name">
        <html><font color="#001aff">Multi</font></html>
        <html><font color="#ff0044">color</font></html>
        <html><font color="#e9c309">Text </font></html>
    </string>
</resources>

enter image description here

此岸叶落 2024-11-11 19:23:16
<android.support.v7.widget.Toolbar
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:id="@+id/toolbar"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="@color/color_primary"
  android:theme="@style/GalaxyZooThemeToolbarDarkOverflow"
  app:popupTheme="@style/Theme.AppCompat.NoActionBar" />


<style name="GalaxyZooThemeToolbarDarkOverflow" parent="Theme.AppCompat.NoActionBar">
  <!-- android:textColorPrimary is the  color of the title text
       in the Toolbar, in the Theme.AppCompat theme:  -->
  <item name="android:textColorPrimary">@color/abc_primary_text_material_light</item>
 
  <!-- android:textColorPrimaryInverse is the  color of the title
       text in the Toolbar, in the Theme.AppCompat.Light theme:  -->
  <!-- <item name="android:textColorPrimaryInverse">@color/abc_primary_text_material_light</item> -->
 
  <!-- android:actionMenuTextColor is the color of the text of
        action (menu) items in the Toolbar, at least in the
        Theme.AppCompat theme.
        For some reason, they already get the textColorPrimary
        when running on API 21, but not on older versions of
        Android, so this is only necessary to support older
        Android versions.-->
        <item name="actionMenuTextColor">@color/abc_primary_text_material_light</item>
  <!-- android:textColorSecondary is the color of the menu
       overflow icon (three vertical dots) -->
  <item name="android:textColorSecondary">@color/abc_secondary_text_material_light</item>
 
  <!-- This would set the toolbar's background color,
        but setting this also changes the popup menu's background,
        even if we define popupTheme for our <Toolbar> -->
  <!-- <item name="android:background">@color/color_primary</item> -->
</style>


**Reference:**
[https://www.murrayc.com/permalink/2014/10/28/android-changing-the-toolbars-text-color-and-overflow-icon-color/][1]
<android.support.v7.widget.Toolbar
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:id="@+id/toolbar"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="@color/color_primary"
  android:theme="@style/GalaxyZooThemeToolbarDarkOverflow"
  app:popupTheme="@style/Theme.AppCompat.NoActionBar" />


<style name="GalaxyZooThemeToolbarDarkOverflow" parent="Theme.AppCompat.NoActionBar">
  <!-- android:textColorPrimary is the  color of the title text
       in the Toolbar, in the Theme.AppCompat theme:  -->
  <item name="android:textColorPrimary">@color/abc_primary_text_material_light</item>
 
  <!-- android:textColorPrimaryInverse is the  color of the title
       text in the Toolbar, in the Theme.AppCompat.Light theme:  -->
  <!-- <item name="android:textColorPrimaryInverse">@color/abc_primary_text_material_light</item> -->
 
  <!-- android:actionMenuTextColor is the color of the text of
        action (menu) items in the Toolbar, at least in the
        Theme.AppCompat theme.
        For some reason, they already get the textColorPrimary
        when running on API 21, but not on older versions of
        Android, so this is only necessary to support older
        Android versions.-->
        <item name="actionMenuTextColor">@color/abc_primary_text_material_light</item>
  <!-- android:textColorSecondary is the color of the menu
       overflow icon (three vertical dots) -->
  <item name="android:textColorSecondary">@color/abc_secondary_text_material_light</item>
 
  <!-- This would set the toolbar's background color,
        but setting this also changes the popup menu's background,
        even if we define popupTheme for our <Toolbar> -->
  <!-- <item name="android:background">@color/color_primary</item> -->
</style>


**Reference:**
[https://www.murrayc.com/permalink/2014/10/28/android-changing-the-toolbars-text-color-and-overflow-icon-color/][1]
爱本泡沫多脆弱 2024-11-11 19:23:16

我们需要使用 app:theme 属性在工具栏中定义主题,如下代码片段所示。

<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="@style/AppTheme.ToolbarFont" />

在此之前,我们必须在 styles.xml 文件中添加主题,并且必须在该样式中定义 font-family,如下面的代码片段所示。

<style name="AppTheme.ToolbarFont" parent="AppTheme">
<!--This line changes the color of text in Toolbar-->
<item name="android:textColorPrimary">@color/black</item>
<!--This line changes the color of icons in toolbar (back, overflow menu icons)-->
<item name="android:textColorSecondary">@color/azul</item>
<item name="textAllCaps">false</item>
<item name="android:textSize">16sp</item>
<item name="android:fontFamily">@font/montserrat_semi_bold</item>

要添加自定义字体,我们需要在 res 目录中创建一个名为“font”的文件夹,如下图所示。

输入图片此处描述

我们在工具栏中实现了自定义字体。

输入图片此处描述

We need to define the theme in our toolbar with app:theme attribute like the below code snippet.

<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="@style/AppTheme.ToolbarFont" />

Before that, we have to add the theme in our styles.xml file and we have to define font-family in that style like the below code snippet.

<style name="AppTheme.ToolbarFont" parent="AppTheme">
<!--This line changes the color of text in Toolbar-->
<item name="android:textColorPrimary">@color/black</item>
<!--This line changes the color of icons in toolbar (back, overflow menu icons)-->
<item name="android:textColorSecondary">@color/azul</item>
<item name="textAllCaps">false</item>
<item name="android:textSize">16sp</item>
<item name="android:fontFamily">@font/montserrat_semi_bold</item>

To add custom fonts we need to create a folder with the name “font” in the res directory like the below screenshot.

enter image description here

We have achieved a custom font in our toolbar.

enter image description here

dawn曙光 2024-11-11 19:23:16

如果您使用自定义操作栏,您可以使用这些属性:

app:titleTextColor="@color/...."
app:subtitleTextColor="@color/...." 
或通过如下方法:
setTitleTextColor()

有关更多属性,请检查此 链接

If you are using a custom action bar you can use these properties:

app:titleTextColor="@color/...."
app:subtitleTextColor="@color/...." 
or by a method like this:
setTitleTextColor()

for more properties check this link

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文