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

10

我已经下载了一个自定义字体用于我的应用程序。我想为该字体设置加粗样式。我尝试使用以下代码,但它没有生效:

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个回答

10

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

这是正确的写法。

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

1
这个完美地运行了,本该被采纳的答案。 - technophyle

4

试试这个

tv.setTypeface(null, Typeface.BOLD);

还有一件事,能否在XML布局中使用?如果可以,怎么做呢? - Dhanesh
在你的XML文件中,为TextView设置android:textStyle="bold"。 - Vamshi
26
这怎么可能是解决方案?它甚至没有指定自定义字体。 - erdomester
2
字体 tf 是如何变粗的? - Harshad

2

这两个答案对我都没有用。

也许它们对别人有用,但是我在我的程序中想要使用粗体字体的版本,我采取了以下步骤:

  1. Copy/pasted the font .ttc - in this case AmericanTypewriter.ttc - to a folder I created in my main/assets/ directory called /fonts. So, main/assets/fonts/AmericanTypewriter.ttc

  2. I made sure I had a TextView with an id in my xml:

    <TextView
        android:id="@+id/myTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is American Bold"/>
    
  3. At the top of my Activity, declared a TextView object:

    private TextView myTextView;
    
  4. In the same Activity's onCreate(), inserted the following code:

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

2
晚来的回答,但也许会有所帮助:
textView.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/BRADHITC.otf"), Typeface.BOLD);

我已经改变了SlidingTabLayoutTextView外观。


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