【问题标题】:Color of title text doesn't change and insert logo on ActionBar标题文本的颜色不会改变并在 ActionBar 上插入徽标
【发布时间】:2014-12-21 08:45:08
【问题描述】:

如果我之前问过这个问题,我会冒代表的风险,但我对 Android(或只是我自己)感到非常沮丧。我正在尝试将我的 ActionBar 上的标题文本的颜色从白色更改为红色,并在文本旁边插入一个徽标,但似乎没有任何改变。

我已经搜索并搜索了 S.O.并尝试了许多解决方案,例如:

How to change color of ActionBar's Title Text on Android 4.3 api 18

Change the title text color in android action bar via xml

但对我没有任何作用。我希望有人能指出我缺乏理解。

这是我的清单 sn-p:

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="21" />

<application
    android:allowBackup="true"
    android:icon="@drawable/small_bc_logo"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name=".BckmMainActivity"
        android:label="@string/app_name" >

我的 styles.xml (res/values-14),基本上是从我查看的第一个链接中复制的:

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">

    <item name="android:actionBarStyle">@style/ActionBarStyle</item>
    <!-- API 14 theme customizations can go here. -->
    <item name="android:textColor">@color/black</item>
    <item name="android:icon">@drawable/small_bc_logo</item>
</style>

<style name="ActionBarStyle" parent="@style/Widget.AppCompat.Light.ActionBar.Solid">
    <item name="android:titleTextStyle">@style/TitleBarTextColor</item>
</style>

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

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

我的颜色.xml:

<resources>
  <color name="red">#A52421</color>
  <color name="black">#000000</color>
</resources>

我的字符串.xml:

<resources>

  <string name="app_name">BCKM</string>
  <string name="action_settings">Settings</string>

</resources>

只是为了添加更多信息我的主要活动类:

public class BckmMainActivity extends ActionBarActivity {

private final Handler handler = new Handler();

private PagerSlidingTabStrip tabs;
private ViewPager pager;
private MyPagerAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bckm_main);

    tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
    pager = (ViewPager) findViewById(R.id.pager);
    adapter = new MyPagerAdapter(getSupportFragmentManager());

    pager.setAdapter(adapter);

    final int pageMargin = (int) TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, 4, getResources()
                    .getDisplayMetrics());
    pager.setPageMargin(pageMargin);

    tabs.setViewPager(pager);
}

...
...
...

非常感谢。

【问题讨论】:

  • allen 尝试将您的样式代码移动到 values 文件夹下的样式文件(而不是 values-14)可能会解决问题,如果您在样式中遇到类似 requires API level 的任何错误,请使用 tools:targetApi 了解更多信息看到stackoverflow.com/questions/15339150/…

标签: android android-actionbar-compat android-actionbaractivity


【解决方案1】:

使用样式设置 appcompat 图标和标题文本颜色:

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="actionBarStyle">@style/MyActionBar</item>
    <item name="navigationIcon">@drawable/ic_launcher</item>
</style>

<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid">
    <item name="titleTextStyle">@style/TitleTextStyle</item>
    <item name="displayOptions">showHome|showTitle</item>
</style>

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

以编程方式设置 appcompat 图标和标题文本颜色:

要设置 ActionBar 标题的颜色,您需要使用 SpannableString。该图标未显示,因为 appcompat-v7 默认将 setDisplayShowHomeEnabled 设置为 false。调用super.onCreate(Bundle)后添加如下代码:

SpannableString spannableString = new SpannableString(getString(R.string.app_name));
spannableString.setSpan(new ForegroundColorSpan(Color.RED), 0, spannableString.toString()
        .length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher);
getSupportActionBar().setTitle(spannableString);

【讨论】:

  • 非常感谢!您的解决方案“立即生效”!我不知道 SpannableString,我可能应该研究一下。您对图标的解释绝对是我所需要的。我试图更改标题文本的颜色并使用styles.xml 插入一个图标。有没有办法这样做?
  • 我还没有尝试过为 appcompat-v7 设置样式(我不使用 appcompat 但已经阅读了大部分源代码)。 appcompat 中的样式是个怪物 :( 我会看看我能不能想出一些东西。
  • @Jared,感谢 Styles 的回答。但是,使用您的样式,它对我不起作用。我的设置一定有问题。我什至创建了不同版本的 values-vXX。没有任何工作。也许只是来晚了...... :-(
  • @Jared,The Styles 现在对我有用。 :-) 我现在不知道为什么,但我会继续使用它。您的两个答案都是 100% 正确的,因此我将其标记为可接受的答案。再次感谢!
【解决方案2】:

这将改变文本颜色:

ActionBar ab = getActionBar();
ab.setTitle(Html.fromHtml("<font color='#ff0000'>YourTitleHere </font>"));

【讨论】:

  • 谢谢。我也喜欢您的解决方案,因为它确实将标题文本变为红色。它给了我希望!但是,我无法让 ab.setIcon 工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-08
相关资源
最近更新 更多