2012-01-12 116 views
6

我已下載自定義字體以在我的應用程序上使用。我想爲該字體設置粗體。我用下面的代碼,但它不工作:如何設置粗體樣式的自定義字體

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

回答

3

試試這個

tv.setTypeface(null, Typeface.BOLD); 
+0

謝謝,它的工作現在.. – Dhanesh 2012-01-12 08:57:16

+0

好吧,如果它工作接受答案 – Vamshi 2012-01-12 08:57:45

+0

還有一件事,是否可以在xml佈局中使用?如果是,那麼如何? – Dhanesh 2012-01-12 09:02:40

8

DEFAULT_BOLD的類型字體的。 Typeface.create()需要int。

這是正確的

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

我試過這個,不工作 – HemangNirmal 2014-09-27 04:50:30

+1

這是完美的工作。應該是被接受的答案。 – technophyle 2015-08-26 01:38:15

0

無論這些答案爲我工作。

也許他們工作了別人,但我在我的程序工作的字體的粗體版本的道,我做了以下內容:

  1. 複製/粘貼的字體.ttc - 在這種情況下AmericanTypewriter .ttc - 到我在名爲/ fonts的main/assets /目錄中創建的文件夾。所以,主/資產/字體/ AmericanTypewriter.ttc

  2. 我確信我有一個TextView在我的XML的ID:

    <TextView 
        android:id="@+id/myTextView" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="This is American Bold"/> 
    
  3. 在我的活動上,宣佈一個TextView對象:

    private TextView myTextView; 
    
  4. 在相同的活性的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); 
    
1

晚的答案,但也許有幫助:

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

這樣,我已經改變了SlidingTabLayout TextView的外觀。

相關問題