Java 设置操作栏标题颜色时不推荐使用Html.fromHtml

Java 设置操作栏标题颜色时不推荐使用Html.fromHtml,java,android,android-studio,android-actionbar,Java,Android,Android Studio,Android Actionbar,将目标SDK版本更新到24.0.0后,fromHtml将被删除,并返回弃用警告。在设置操作栏的标题时,需要更改哪些内容才能解决此错误 最低空气污染指数为17 actionBar.setTitle(Html.fromHtml("<font color='#FFFFFF'>" + getResources().getString(R.string.welcome) + "</font>")); actionBar.setTitle(Html.fromHtml(“+getRe

将目标SDK版本更新到24.0.0后,
fromHtml
将被删除,并返回弃用警告。在设置操作栏的标题时,需要更改哪些内容才能解决此错误

最低空气污染指数为17

actionBar.setTitle(Html.fromHtml("<font color='#FFFFFF'>" + getResources().getString(R.string.welcome) + "</font>"));
actionBar.setTitle(Html.fromHtml(“+getResources().getString(R.string.welcome)+”);
不推荐使用“fromHtml(java.lang.String)”


如果您的
minSdkVersion
为24或更高,请使用。好的,
FROM\u HTML\u MODE\u LEGACY
将是用于与旧的无标志
fromHtml()
兼容的标志值


如果您的
minSdkVersion
低于24,您可以选择:

  • 始终使用
    fromHtml()
    您是,可能使用快速修复(Alt-Enter)来抑制Lint警告

  • 使用两个版本的
    fromHtml()
    :一个是在API级别24+的设备上运行应用程序时使用标志的版本,另一个是在旧设备上不使用标志的版本

后者看起来像:

if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.N) {
  actionBar.setTitle(Html.fromHtml(..., Html.FROM_HTML_MODE_LEGACY));
}
else {
  actionBar.setTitle(Html.fromHtml(...));
}
(其中,
是要转换的HTML)


但是请注意,如果您只是想更改整个操作栏标题的颜色,请使用
Html.fromHtml()
和类似的
span
解决方案适用于单个
TextView
(或使用
TextView
,如操作栏)中不同文本段需要不同颜色的情况。

如果
minSdkVersion
为24或更高,请使用。好的,
FROM\u HTML\u MODE\u LEGACY
将是用于与旧的无标志
fromHtml()
兼容的标志值


如果您的
minSdkVersion
低于24,您可以选择:

  • 始终使用
    fromHtml()
    您是,可能使用快速修复(Alt-Enter)来抑制Lint警告

  • 使用两个版本的
    fromHtml()
    :一个是在API级别24+的设备上运行应用程序时使用标志的版本,另一个是在旧设备上不使用标志的版本

后者看起来像:

if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.N) {
  actionBar.setTitle(Html.fromHtml(..., Html.FROM_HTML_MODE_LEGACY));
}
else {
  actionBar.setTitle(Html.fromHtml(...));
}
(其中,
是要转换的HTML)


但是请注意,如果您只是想更改整个操作栏标题的颜色,请使用<基于code>Html.fromHtml()和类似的
span
的解决方案适用于在单个
TextView
(或使用
TextView
,如操作栏)中的不同文本段需要不同颜色的情况。

您应该使用其他方法

如果使用工具栏:

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

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

@style/MyTheme.ActionBarStyle
@style/MyTheme.ActionBar.TitleTextStyle
@颜色/红色

在这里找到更多方法:

您应该使用其他方法

如果使用工具栏:

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

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

@style/MyTheme.ActionBarStyle
@style/MyTheme.ActionBar.TitleTextStyle
@颜色/红色

在这里找到更多方法:

您可以而且应该使用
android.support.v7.widget.Toolbar
。检查这里:您可以并且应该使用
android.support.v7.widget.Toolbar
。请在此处查看: