7

如何使用 ActionBarSherlock 的主题更改操作栏标题的字体大小?

我目前的主题(应用于我的整个应用程序如下):

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

请注意,字体大小设置为我应用程序中所有视图的设置。只是不是 ActionBar 的字体。

4

2 回答 2

10

如果您只需要更改标题的样式,您可以添加一个自定义视图,如下所示:

<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" />

然后隐藏实际标题并显示自定义视图。

    _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);

就这样!如果您对 ActionBar 标题的默认字体大小感兴趣,它是 18sp。

于 2012-06-27T07:38:43.827 回答
7

我通过使用以下主题得到了这个工作:

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

请注意,文本大小采用名为 的样式MyApp.ActionBar.TextAppearance。同样根据ActionBarSherlock主题应该设置在 android 前缀属性和普通属性上(参见上面样式 Theme.MyApp.Default 中的 actionBarStyle。)

于 2012-06-27T06:17:00.747 回答