简体   繁体   中英

How can I change the font style on ActionBarSherlock

How do I change the font size for the title of the Action bar using Themes for ActionBarSherlock?

My theme at the moment (applied to my whole application is as follows):

<style name="Theme.MyAppDefaults" parent="@style/Theme.Sherlock.Light">
         <item name="android:textSize">@dimen/default_textsize</item>
         <item name="actionBarSize">@dimen/action_bar_height</item>
</style>

Notice that the font size is set as set on all the Views in my app. Just not the ActionBar's font.

If you need to just change the title's style you can add a custom view with like this:

<TextView
    android:id="@+id/action_custom_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="My Custom title"
    android:textColor="#fff"
    android:textSize="18sp" />

Then hide the actual title and show the custom view.

    _actionBar.setDisplayShowTitleEnabled(false);

     LayoutInflater inflater = LayoutInflater.from(this);
     View customView = inflater.inflate(R.layout.custom_action_layout, null);

     TextView titleTV = (TextView) customView.findViewById(R.id.action_custom_title);
     // you can apply a custom typeface here or do sth else...

     _actionBar.setCustomView(customView);
     _actionBar.setDisplayShowCustomEnabled(true);

That's all! If you are interested what is the default font size for ActionBar title it's 18sp.

I got this working by using the below theme:

<style name="Theme.MyApp.Default" parent="@style/Theme.Sherlock.Light">
       <item name="actionBarStyle">@style/MyApp.SherlockActionBarStyle</item>
       <item name="android:actionBarStyle">@style/MyApp.SherlockActionBarStyle</item>
</style>

<style name="MyApp.SherlockActionBarStyle" parent="@style/Widget.Sherlock.ActionBar">
       <item name="titleTextStyle">@style/MyApp.ActionBar.TextAppearance</item>
</style>

<style name="MyApp.ActionBar.TextAppearance" parent="@style/TextAppearance.Sherlock.Widget.ActionBar.Menu">
      <item name="android:textSize">@dimen/default_textsize</item>
      <item name="android:textColor">@color/dark_grey</item>
 </style>

Notice the Text size is in the style named MyApp.ActionBar.TextAppearance . Also according to ActionBarSherlock the theme should be set on both android prefixed attributes and normal attributes (See actionBarStyle in style Theme.MyApp.Default above.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM