如何在 android 中更改部分操作栏的颜色
how to change color of part of actionbar in android
我的操作栏标题是 2 个词,我想为每个词使用 2 种颜色,如下图所示:
现在我正在使用这些代码来更改文本颜色:
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:actionBarSize">45dp</item>
<item name="actionBarSize">45dp</item>
<item name="android:actionBarStyle">@style/MyActionBar</item>
<item name="actionBarStyle">@style/MyActionBar</item>
<item name="android:windowActionBarOverlay">true</item>
</style>
<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar">
<item name="android:background">@color/actionbar</item>
<item name="background">@color/actionbar</item>
<item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
<item name="android:subtitleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
<item name="titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
<item name="subtitleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
</style>
<style name="MyTheme.ActionBar.TitleTextStyle" parent="@android:style/TextAppearance">
<item name="android:textColor">@color/white</item>
<item name="android:textSize">15sp</item>
</style>
但是这些代码改变了两个字的颜色,那我该怎么做呢?
为您的字幕创建一个单独的样式。
在底部添加:
<style name="MyTheme.ActionBar.SubTitleTextStyle" parent="android:style/TextAppearance">
<item name="android:textColor">@color/black</item>
<item name="android:textSize">12sp</item>
</style>
然后改变
<item name="@android:subtitleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
<item name="subtitleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
至
<item name="android:subtitleTextStyle">@style/MyTheme.ActionBar.SubTitleTextStyle</item>
<item name="subtitleTextStyle">@style/MyTheme.ActionBar.SubTitleTextStyle</item>
尝试使用 spannable
String part1 = "Actionbar ";
String part2 = "TITLE";
SpannableString s = new SpannableString(part1 + part2);
s.setSpan(new ForegroundColorSpan(Color.RED), part1.length(), part1.length()+part2.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// Update the action bar title with the spannable instance
getActionBar().setTitle(s);
把它放在你的 oncreate()
String text = "<font color='#F44336'>Your</font><font color='#FF9800'>Title</font>";
getSupportActionBar().setTitle(Html.fromHtml(text));
我的操作栏标题是 2 个词,我想为每个词使用 2 种颜色,如下图所示:
现在我正在使用这些代码来更改文本颜色:
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:actionBarSize">45dp</item>
<item name="actionBarSize">45dp</item>
<item name="android:actionBarStyle">@style/MyActionBar</item>
<item name="actionBarStyle">@style/MyActionBar</item>
<item name="android:windowActionBarOverlay">true</item>
</style>
<style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar">
<item name="android:background">@color/actionbar</item>
<item name="background">@color/actionbar</item>
<item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
<item name="android:subtitleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
<item name="titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
<item name="subtitleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
</style>
<style name="MyTheme.ActionBar.TitleTextStyle" parent="@android:style/TextAppearance">
<item name="android:textColor">@color/white</item>
<item name="android:textSize">15sp</item>
</style>
但是这些代码改变了两个字的颜色,那我该怎么做呢?
为您的字幕创建一个单独的样式。 在底部添加:
<style name="MyTheme.ActionBar.SubTitleTextStyle" parent="android:style/TextAppearance">
<item name="android:textColor">@color/black</item>
<item name="android:textSize">12sp</item>
</style>
然后改变
<item name="@android:subtitleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
<item name="subtitleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
至
<item name="android:subtitleTextStyle">@style/MyTheme.ActionBar.SubTitleTextStyle</item>
<item name="subtitleTextStyle">@style/MyTheme.ActionBar.SubTitleTextStyle</item>
尝试使用 spannable
String part1 = "Actionbar ";
String part2 = "TITLE";
SpannableString s = new SpannableString(part1 + part2);
s.setSpan(new ForegroundColorSpan(Color.RED), part1.length(), part1.length()+part2.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// Update the action bar title with the spannable instance
getActionBar().setTitle(s);
把它放在你的 oncreate()
String text = "<font color='#F44336'>Your</font><font color='#FF9800'>Title</font>";
getSupportActionBar().setTitle(Html.fromHtml(text));