eclipse 在android中更改应用程序标题的字体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17966350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 21:00:00  来源:igfitidea点击:

Changing the font of the application title in android

javaandroideclipsetitlebar

提问by anilbey

I have a typeface, I want to change the font of the action-bar title in android. Is there a way to set title typeface like that?

我有一个字体,我想更改 android 中操作栏标题的字体。有没有办法设置这样的标题字体?

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

this is not a copy question, those methods on this link (How to Set a Custom Font in the ActionBar Title?) are not working. When I try them, eclipse gives that error : java.lang.noSuchMethodError

这不是复制问题,此链接上的那些方法(如何在 ActionBar 标题中设置自定义字体?)不起作用。当我尝试它们时,eclipse 给出了那个错误:java.lang.noSuchMethodError

回答by Karthikeyan

Try this,

尝试这个,

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

If it's toolbar means try like as below.

如果是工具栏意味着尝试如下。

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

Then just add any style via programmatically using below comment.

然后只需使用以下评论以编程方式添加任何样式。

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

OR

或者

Change using style. Need one info click here.

改变使用风格。需要一个信息点击这里

回答by Christopher Suárez

I found this answerfrom headsvk that helped me. Doing some adjustments I only needed to do this:

我从 Headsvk找到了这个对我有帮助的答案。做一些调整我只需要这样做:

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

Doing some tests on my app I found that the title TextView was the first child of the toolbar, so now that I have it I can set changes to it.

对我的应用程序进行一些测试时,我发现标题 TextView 是工具栏的第一个子项,所以现在我有了它,我可以对其进行更改。

回答by canova

https://stackoverflow.com/a/8748802/1943671you can do like in this answer.

https://stackoverflow.com/a/8748802/1943671你可以像在这个答案中那样做。

After setting your custom view to the Action Bar like in the link, just give the TextView the typeface:

像链接中一样将自定义视图设置为操作栏后,只需为 TextView 指定字体:

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

回答by vs.thaakur

It is not supported.But you can create custom view for your action bar. How to Set a Custom Font in the ActionBar Title?

它不受支持。但您可以为您的操作栏创建自定义视图。如何在 ActionBar 标题中设置自定义字体?

@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;
}

and in your menifest

在你的清单中

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

回答by Andy Res

You can do that by creating a custom view and inflating that custom view in the ActionBar.
This custom view will contain a TextViewthat you'll get a reference to, and then be able to set a typeface.

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

See an example here: How to Set a Custom Font in the ActionBar Title?

请参阅此处的示例:如何在 ActionBar 标题中设置自定义字体?