如何以粗体样式设置自定义字体

时间:2012-01-12 08:47:39

标签: android android-layout android-widget

我已下载要在我的应用上使用的自定义字体。我想为该字体设置粗体样式。我使用了以下代码,但它不起作用:

Typeface tf = Typeface.createFromAsset(getAssets(),
"fonts/BRADHITC.otf");
Typeface bold = Typeface.create(tf, Typeface.DEFAULT_BOLD);    
TextView tv = (TextView) findViewById(R.id.cou_text);
tv.setTypeface(tf);

4 个答案:

答案 0 :(得分:10)

DEFAULT_BOLD是Typeface类型。 Typeface.create()需要int。

这是正确的

Typeface bold = Typeface.create(tf, Typeface.BOLD);  

答案 1 :(得分:4)

试试这个

tv.setTypeface(null, Typeface.BOLD);

答案 2 :(得分:2)

这些答案都不适合我。

也许他们为其他人工作,但是我在程序中使用粗体版字体的方式,我做了以下事情:

  1. 将字体.ttc(在本例中为AmericanTypewriter.ttc)复制/粘贴到我在main / assets /目录中创建的名为/ fonts的文件夹中。那么, main / assets / fonts / AmericanTypewriter.ttc

  2. 我确定我的xml中有一个带有id的TextView:

    <TextView
        android:id="@+id/myTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is American Bold"/>
    
  3. 在我的Activity顶部,声明了一个TextView对象:

    private TextView myTextView;
    
  4. 在同一个Activity的onCreate()中,插入以下代码:

    Typeface americanFont = Typeface.createFromAsset(getAssets(),
            "fonts/AmericanTypewriter.ttc");
        Typeface americanFontBold = Typeface.create(americanFont, Typeface.BOLD);
        myTextView = (TextView) findViewById(R.id.myTextView);
        myTextView.setTypeface(americanFontBold);
    

答案 3 :(得分:2)

迟到的回答但也许有帮助:

textView.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/BRADHITC.otf"), Typeface.BOLD);

这样我改变了SlidingTabLayout的TextView外观。

相关问题