繁体   English   中英

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

[英]Changing the font of the application title in android

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

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

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

尝试这个,

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

要么

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

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

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

在我的应用程序上做一些测试我发现标题TextView是工具栏的第一个孩子,所以现在我拥有它我可以设置它的更改。

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

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

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

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

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

它不受支持。但您可以为操作栏创建自定义视图。 如何在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;
}

在你的清单中

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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