在安卓中更改应用标题的字体。

4

我有一种字体,我想在Android中更改操作栏标题的字体。有没有一种设置标题字体的方法?

this.setTitle(myTitle.toUpperCase()); 
this.setTypefaceofTitle(tf);

这不是一个复制问题,这个链接上的那些方法(如何在ActionBar标题中设置自定义字体?)并不能生效。当我尝试使用它们时,Eclipse会报错:java.lang.noSuchMethodError


你是指要在“ActionBar”中更改应用程序的标题吗? - Andy Res
是的,我正在编辑问题。 - anilbey
5个回答

15

试一下这个,

    int actionBarTitle = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
TextView actionBarTitleView = (TextView) getWindow().findViewById(actionBarTitle);
Typeface robotoBoldCondensedItalic = Typeface.createFromAsset(getAssets(), "fonts/Roboto-BoldCondensedItalic.ttf");
if(actionBarTitleView != null){
    actionBarTitleView.setTypeface(robotoBoldCondensedItalic);
}

如果工具栏无法显示,请尝试以下操作。

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="@color/action_color”
app:theme="@style/ToolBarTheme" >


 <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Title"
    android:layout_gravity="center"
    android:id="@+id/title_txt” />


</android.support.v7.widget.Toolbar>

然后只需通过以下注释以编程方式添加任何样式。

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
TextView mTitle = (TextView) toolbar.findViewById(R.id.title);

或者

使用样式进行更改。需要一个信息,请点击这里


好的解决方案。一旦您拥有了TextView,您可以按照自己的喜好进行样式设置。 - PeteH
工作得非常完美,而且非常简单。希望它是未来可靠的? - TheLettuceMaster
整洁的解决方案,现在您完全掌控文本及其外观。 - Vahid Amiri

2

我找到了headsvk的这个答案,对我很有帮助。做了一些调整后,我只需要执行以下操作:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
...
TextView myTitle = (TextView) toolbar.getChildAt(0);
myTitle.setTypeface(font);

在测试我的应用程序时,我发现标题 TextView 是 toolbar 的第一个子项,因此现在我可以对其进行更改。


1

https://dev59.com/N2oy5IYBdhLWcg3wWsw_#8748802 你可以像这个答案中那样操作。

在像链接中那样将自定义视图设置到操作栏之后,只需为TextView设置字体即可:

Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/yourfont.ttf");
TextView tv = (TextView) findViewById(R.id.customTitle);
tv.setTypeface(tf);

1
如何找到我的操作栏标题的ID?显然,它不是customTitle。 - anilbey
2
那个页面上的答案:“https://dev59.com/N2oy5IYBdhLWcg3wWsw_#8748802”不起作用。 - anilbey

0

不支持该功能。但是您可以为操作栏创建自定义视图。如何在操作栏标题中设置自定义字体?

@Override
public boolean onCreateOptionsMenu(Menu menu) {


    //getMenuInflater().inflate(R.menu.activity_main, menu);
    this.getActionBar().setDisplayShowCustomEnabled(true);
    this.getActionBar().setDisplayShowTitleEnabled(false);
    Typeface tf = Typeface.createFromAsset(getAssets(),
            "fonts/architectsdaughter.ttf");


    LayoutInflater inflator = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflator.inflate(R.layout.xml_master_footer, null);


    this.getActionBar().setCustomView(v);

    ((TextView)v.findViewById(R.id.title)).setText(this.getTitle());
    ((TextView) v.findViewById(R.id.title)).setTypeface(tf);


    //assign the view to the actionbar

    return true;
}

同时在你的清单文件中

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="16" />

1
这个链接上的那些方法(如何在ActionBar标题中设置自定义字体?)不起作用。当我尝试它们时,Eclipse会报错:java.lang.noSuchMethodError。 - anilbey
它需要最低API版本11。请检查您的版本。 - vs.thaakur

0

您可以通过创建自定义视图并在 ActionBar 中填充该自定义视图来实现此目的。
这个自定义视图将包含一个 TextView,您将获得对它的引用,然后能够设置字体。

在这里查看示例:如何在 ActionBar 标题中设置自定义字体?


在发布问题之前,我已经尝试过了。那个页面上的解决方案都不起作用 :( - anilbey
最好的方法是发布你的代码。也许有人能够找出为什么它不起作用。 - Andy Res
但是,我的代码与那个主题无关。我想做的就是这样, setTitleTypeface(myTypeface); - anilbey

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