簡體   English   中英

更改 ActionBar 中標題的顏色

[英]Change color of the title in ActionBar

我可以知道如何根據下面的 xml 代碼將 ActionBar 中標題的顏色更改為黑色嗎? 我已經嘗試了幾種找到的方法,但它並沒有真正起作用。

下面是樣式 xml 代碼的一部分:::

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

<style name="TestingBaseTheme" parent="Theme.AppCompat.Light">
    <item name="android:actionBarStyle">@style/ActionBarTheme</item>
    <item name="android:actionBarTabTextStyle">@style/ActionBarTabText</item>
    <item name="android:actionMenuTextColor">@color/white</item> 
    <item name="android:textColor">@color/black</item>
</style>

<style name="ActionBarTheme" parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
    <item name="android:background">@color/white</item>
</style>

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

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

這是答案:點擊下面的鏈接

操作欄文本顏色

好的,我找到了更好的方法。 我現在只能更改標題的顏色,您還可以調整副標題。

這是我的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>

我找到了最簡單最簡單的代碼

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

如果我沒記錯的話,設置 ActionBar 文本樣式的屬性是

 <item name="android:titleTextStyle" tools:ignore="NewApi"

我還注意到您正在使用 ActionBarCompat。 如果您的目標是預 HoneyComb 設備,您還必須添加相同的屬性,但不帶android:前綴

 <item name="titleTextStyle" tools:ignore="NewApi"

在您的活動中設置 android 標簽 android:label="name"

您可以使用此代碼片段:

SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
SpannableString spannableString = new SpannableString(getSupportActionBar().getTitle()+"");
spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(MainActivity.this, R.color.yellow)), 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableStringBuilder.append(spannableString);
getSupportActionBar().setTitle(spannableStringBuilder);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM