【问题标题】:Changing the font of the application title in android在android中更改应用程序标题的字体
【发布时间】:2013-07-31 09:19:17
【问题描述】:

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

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

这不是复制问题,此链接 (How to Set a Custom Font in the ActionBar Title?) 上的那些方法不起作用。 当我尝试它们时,eclipse给出了那个错误:java.lang.noSuchMethodError

【问题讨论】:

  • 你的意思是在ActionBar的应用上改标题吗?
  • 是的,我正在编辑问题。

标签: java android eclipse titlebar


【解决方案1】:

试试这个,

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

改变使用风格。需要一个信息click here

【讨论】:

  • 很好的解决方案。拥有 TextView 后,您可以随意设置样式。
  • 工作完美,如此简单。希望它在未来得到验证?
  • 简洁的解决方案,现在您可以完全控制文本及其显示方式。
【解决方案2】:

我从 headvk 找到了对我有帮助的 this answer。做一些调整我只需要这样做:

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

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

    【解决方案4】:

    您可以通过创建自定义视图并在 ActionBar. 中扩展该自定义视图来做到这一点
    此自定义视图将包含一个 TextView,您将获得对其的引用,然后可以设置字体。

    在此处查看示例:How to Set a Custom Font in the ActionBar Title?

    【讨论】:

    • 在发布我的问题之前我已经尝试过了。该页面上的那些解决方案不起作用:(
    • 最好是发布您的代码。也许有人可以弄清楚为什么它不起作用。
    • 但是,我的代码与该主题无关。我想做的就是,setTitleTypeface(myTypeface);
    【解决方案5】:

    不支持。但您可以为操作栏创建自定义视图。 How to Set a Custom Font in the ActionBar Title?

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

    【讨论】:

    • 此链接上的那些方法(如何在 ActionBar 标题中设置自定义字体?)不起作用。当我尝试它们时,eclipse给出了那个错误:java.lang.noSuchMethodError
    • 它需要最低 API 版本 11。请检查你的
    猜你喜欢
    • 1970-01-01
    • 2015-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-22
    • 2013-07-03
    • 2020-02-14
    • 2015-09-22
    相关资源
    最近更新 更多