How to set bold title in Action Bar?

家住魔仙堡 提交于 2019-12-19 05:23:19

问题


I am editing the style xml trying to get the Activity title bold.

<style name="MyTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
</style>

<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:background">#81CFEB</item>
    <item name="android:textStyle">bold</item>
</style>

But only what I can set is the background color desired. I do not why the textStyle is not set to bold.

Anyone know how to solve it?


回答1:


Can you please try with this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="MyTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/MyTheme.MyActionBar</item>
  </style>

  <style name="MyTheme.MyActionBar"parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:titleTextStyle">@style/MyTheme.MyActionBar.TitleTextStyle</item>
  </style>

  <style name="MyTheme.MyActionBar.TitleTextStyle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:background">#81CFEB</item>
    <item name="android:textStyle">bold</item>
  </style>
</resources>



回答2:


For those using AppCompat, you'll need something like this:

<style name="MyTheme" parent="Theme.AppCompat">
    <item name="android:actionBarStyle">@style/MyTheme.ActionBar</item>
    <item name="actionBarStyle">@style/MyTheme.ActionBar</item>
</style>

<style name="MyTheme.ActionBar" parent="Widget.AppCompat.ActionBar.Solid">
    <item name="titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
</style>

<style name="MyTheme.ActionBar.TitleTextStyle" parent="TextAppearance.AppCompat">
    <item name="android:background">#81CFEB</item>
    <item name="android:textStyle">bold</item>
</style>



回答3:


if (Build.VERSION.SDK_INT >= 24) {
    getSupportActionBar().setTitle(Html.fromHtml("<b>" + title + "</b>", 0));
    }else {
    getSupportActionBar().setTitle(Html.fromHtml("<b>" + title + "</b>"));
    }


来源:https://stackoverflow.com/questions/18969629/how-to-set-bold-title-in-action-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!