如何在安卓中更改ActionBar标题和副标题的字体大小?

3

我尝试了这个,但没有任何反应。

<style name="MyCustomTheme" parent="Theme.Freshchat.Light.DarkActionBar">
    <item name="titleTextStyle">@style/Style1</item>
</style>

<style name="Style1" parent="@android:style/Widget.TextView">
    <item name="android:textSize">12sp</item>
</style>

更多背景信息,我正在使用Freshchat聊天小部件,希望能够更改标题和副标题的字体大小。谢谢!
4个回答

11

试试这个,你可以使用工具栏

<android.support.v7.widget.Toolbar
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:id="@+id/myToolbar"
  android:layout_width="match_parent"
  android:layout_height="?attr/actionBarSize"
  app:titleTextAppearance="@style/ToolbarTitleTheme"
  app:subtitleTextAppearance="@style/ToolbarSubtitleTheme">
</android.support.v7.widget.Toolbar>

然后在Style.xml中创建样式

<style name="ToolbarTitleTheme" parent="@style/TextAppearance.Widget.AppCompat.Toolbar.Title">
    <item name="android:textSize">20dp</item>
    <<item name="android:textColor">#FFAA12</item>
    <!-- include other attributes you want to change: textColor, textStyle, etc -->
</style>

<style name="ToolbarSubtitleTheme" parent="@style/TextAppearance.Widget.AppCompat.Toolbar.Subtitle">
    <item name="android:textSize">14dp</item>
    <item name="android:textColor">#FFAA12</item>
</style>

3

1>创建一个名为titleview.xml的XML文件。

代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:textSize="20sp"
        android:textColor="@android:color/white"
        android:maxLines="1"
        android:ellipsize="end"
        android:text="" />
</RelativeLayout>

2>在OnCreate方法中:

if(getSupportActionBar() != null) {
            getSupportActionBar().setDisplayShowCustomEnabled(true);
            getSupportActionBar().setDisplayShowTitleEnabled(false);

            LayoutInflater inflator = LayoutInflater.from(this);
            View v = inflator.inflate(R.layout.titleview, null);

//if you need to customize anything else about the text, do it here.
//I'm using a custom TextView with a custom font in my layout xml so all I need to do is set title
            ((TextView) v.findViewById(R.id.title)).setText(this.getTitle());
            ((TextView) v.findViewById(R.id.title)).setTextSize(20);


//assign the view to the actionbar
            this.getSupportActionBar().setCustomView(v);
        }

3>在你的Activity的Manifest文件中添加这行代码

android:label="@string/room_title"

1
简单的方法是创建一个名为custom_action_bar.xml的布局文件,用于您的操作栏,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView
        android:id="@+id/action_bar_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="You title"
        android:textColor="#ff0"
        android:textSize="14sp"
        android:textStyle="bold" />
    <TextView
        android:id="@+id/action_bar_subtitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Your subtitle"
        android:textColor="#ff0"
        android:textSize="12sp" />
</LinearLayout>

那么,您需要将自定义操作栏的布局设置如下:
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    getSupportActionBar().setCustomView(R.layout.custom_action_bar);
}

使用这种方法,您将能够轻松自定义操作栏。

0
你可以按照以下方式使用SpannableString
 SpannableString s = new SpannableString(getResources().getString(R.string.manage_prescription));
    s.setSpan(new TypefaceSpan(YourActivity.this, Constants.TOOLBAR_TITLE_FONT), 0, s.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

使用SpannableString对象将标题设置到您的操作栏中。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接