Android 如何在 ActionBarSherlock 中设置标题颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10352022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:23:38  来源:igfitidea点击:

How to set title color in ActionBarSherlock?

androidactionbarsherlock

提问by Sander Versluys

How to set title color in ActionBarSherlock?

如何设置标题颜色ActionBarSherlock

Which theming attributes should I use?

我应该使用哪些主题属性?

回答by Ajmal Salim

You can easily do this with

你可以很容易地做到这一点

getSupportActionBar().setTitle(Html.fromHtml("<font color='#ffffff'>App Name</font>"));

回答by adneal

From the ActionBarSherlock website

来自ActionBarSherlock 网站

Parent Themes

父主题

In order for the custom action bar implementation to function your application must use Theme.Sherlock, Theme.Sherlock.Light, or Theme.Sherlock.Light.DarkActionBar, or your custom theme must use one of the aforementioned as its parent.

为了使自定义操作栏实现运行,您的应用程序必须使用 Theme.Sherlock、Theme.Sherlock.Light 或 Theme.Sherlock.Light.DarkActionBar,或者您的自定义主题必须使用上述主题之一作为其父主题。

Mirrored Attributes

镜像属性

Due to limitations in Android's theming system any theme customizations must be declared in two attributes. The normal android-prefixed attributes apply the theme to the native action bar and the unprefixed attributes are for the custom implementation...

由于 Android 主题系统的限制,任何主题自定义都必须在两个属性中声明。普通的 android-prefixed 属性将主题应用于本机操作栏,而无前缀的属性用于自定义实现...

In short, that means you need to leave out the android:prefix when you're styling the ActionBar.

简而言之,这意味着android:在设置ActionBar.

Theming the title color

主题化标题颜色

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="YOURTHEME" parent="Theme.Sherlock.Light">
<item name="android:actionBarStyle">@style/YOURTHEME.ActionBarStyle</item>
<item name="actionBarStyle">@style/YOURTHEME.ActionBarStyle</item>
</style>

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

<style name="YOURTHEME.ActionBar.TitleTextStyle" parent="TextAppearance.Sherlock.Widget.ActionBar.Title">
<item name="android:textColor">@color/YOUR_COLOR</item>
<item name="textColor">@color/YOUR_COLOR</item>
</style>
</resources>

NOTE

笔记

I haven't tested this. Also, what have you tried?You should searching before posting on SOin the future.

我没有测试过这个。另外,你试过什么?您应该在将来在SO上发布之前进行搜索。

回答by shontauro

i have changed the title color of this way:

我已经改变了这种方式的标题颜色:

in the Styles.xml file, i have added these styles

在 Styles.xml 文件中,我添加了这些样式

<style name="Theme.Styled" parent="@style/Theme.Sherlock.Light">
    <item name="actionBarStyle">@style/CustomActionBarStyle</item>
    <item name="android:actionBarStyle">@style/CustomActionBarStyle</item>
</style>

<style name="CustomActionBarStyle" parent="@style/Widget.Sherlock.ActionBar">
    <item name="titleTextStyle">@style/CustomTitleColorBar</item>
    <item name="android:titleTextStyle">@style/CustomTitleColorBar</item>
</style>

<!-- Style for the color title bar -->
<style name="CustomTitleColorBar" parent="@style/TextAppearance.Sherlock.Widget.ActionBar.Title">
    <item name="android:textColor">@android:color/white</item>
</style>

the last thing that i did was set the theme in the activity:

我做的最后一件事是在活动中设置主题:

public static int THEME = R.style.Theme_Styled;

public static int THEME = R.style.Theme_Styled;

this line goes before super.onCreate() method

这一行在 super.onCreate() 方法之前

setTheme(THEME);

设置主题(主题);

this is it.

就是这个。

回答by TanvirChowdhury

you can do it from 2 places either in style.xml or programatically
1.From style.xml

您可以从 style.xml 或以编程方式
1.From style.xml 中的2 个位置执行此操作

2.Or in code

2.或者在代码中

int titleId = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");  
TextView yourTextView = (TextView)findViewById(titleId);
yourTextView.setTextColor(getResources().getColor(R.color.myColor));

Or using actionbar you can also achieve this

或者使用 actionbar 你也可以实现这个

getSupportActionBar().setTitle(Html.fromHtml("<font color='#ffffff'>App Name</font>"));

回答by Thiago Pimentel

I think you are trying to change the text color that is on the Menu, not the color of text that is in the Titlein ActionBarSherlock.

我认为您正在尝试更改Menu上的文本颜色,而不是ActionBarSherlockTitle中的文本颜色。

When you use getSupportActionBar().SetTitle("My Title")to style this text you should use the code @Archie.bpgc. His answer is correct, but I believe that you are in need of the following code:

当您使用getSupportActionBar().SetTitle("My Title")样式设置此文本时,您应该使用代码@Archie.bpgc。他的回答是正确的,但我相信您需要以下代码:

<style name="MyTheme.TitleTextStyle" parent="TextAppearance.Sherlock.Widget.ActionBar.Menu">
    <item name="android:textColor">@color/white</item>
    <item name="android:textSize">24sp</item>
    <item name="android:gravity">center</item>
</style>

See that you simply exchange parent =" TextAppearance.Sherlock.Widget.ActionBar.Title">with parent = "TextAppearance.Sherlock.Widget.ActionBar.Menu">

看到你只需换parent =" TextAppearance.Sherlock.Widget.ActionBar.Title">parent = "TextAppearance.Sherlock.Widget.ActionBar.Menu">