简体   繁体   English

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

[英]How to set title color in ActionBarSherlock?

How to set title color in ActionBarSherlock ? 如何在ActionBarSherlock设置标题颜色?

Which theming attributes should I use? 我应该使用哪些主题属性?

您可以轻松地执行此操作

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

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. 由于Android主题系统的限制,任何主题自定义都必须在两个属性中声明。 The normal android-prefixed attributes apply the theme to the native action bar and the unprefixed attributes are for the custom implementation... 正常的android-prefixed属性将主题应用于本机操作栏,未加前缀的属性用于自定义实现...

In short, that means you need to leave out the android: prefix when you're styling the ActionBar . 简而言之,这意味着你需要在设置ActionBar样式时省略android:前缀。

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 SO in the future. 您应该在将来发布到SO之前进行搜索。

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); setTheme(主题);

this is it. 就是这个。

you can do it from 2 places either in style.xml or programatically 你可以在style.xml或编程中的2个地方做到这一点
1.From style.xml 1.从style.xml

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 或者使用操作栏,您也可以实现此目的

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

I think you are trying to change the text color that is on the Menu , not the color of text that is in the Title in ActionBarSherlock . 我认为您正在尝试更改菜单上的文本颜色,而不是ActionBarSherlock标题中的文本颜色。

When you use getSupportActionBar().SetTitle("My Title") to style this text you should use the code @Archie.bpgc . 当您使用getSupportActionBar().SetTitle("My Title")来设置此文本的样式时,您应该使用代码@Archie.bpgc His answer is correct, but I believe that you are in need of the following code: 他的回答是正确的,但我相信你需要以下代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM