2013-03-31 66 views
16

我發現關於這個話題的幾個職位,但所有這一切的主題或者設置字體與setTypeFace()方法TextView對象,或創建該字體設置爲Robotoextends自定義類上TextView。據我所知,從API-Level 11(?)什麼的,我們可以將TypeFace設置爲xml屬性,如何設置。像這樣:在TextView中設置的Roboto字體 - XML

<TextView 
     android:id="@+id/profileHeader" 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     android:typeface="roboto" 
     android:text="Hello, world"> 
    </TextView> 

什麼是正確的方法來做到這一點?是否有可能,如果應用程序比API級別11像一個較低的設備上運行有一個備用的(?):

android:typeface="roboto|monospace|serif" 
+0

我不認爲有什麼方法可以從默認的XML以外的XML中設置Android TextView中的字體。 – Milan

回答

6

看看在RobotoTextView項目。適用於Android 1.5,您可以使用XML屬性設置字體。它還包括其他視圖,如RobotoButton,RobotoCheckbox等。

1

我不明白的方式,你可以定義一個外部的字體作爲XML屬性。您應該將字體存儲在資產,並呼籲:

tv.setTypeface(Typeface.createFromAsset(context.getAssets(), roboto.ttf)); 
1

不能從資產設置字體直接這樣你要做的是遵循的onCreate。這將使你想做的事情變成同樣的東西。

TextView tvTextView = (TextView) findViewById(R.id.textView1); 
Typeface typeface = Typeface.createFromAsset(getAssets(),"Roboto.ttf"); 
tvTextView.setTypeface(typeface); 

希望它會幫助你out.:D

1

android:typeface屬性只有少數有效的選項(根據Android的文檔)...

  • 正常
  • SANS
  • serif
  • 等寬

如果您的應用需要舊版設備的Roboto字體,則需要將Roboto TTF文件包含到您的項目中。

使用這些字體最明顯的方法是使用TextView的setTypeface()方法,但是如果您想用XML指定它,則必須創建自定義TextView併爲您的自定義TextView創建自己的樣式屬性。

This topic is all over the Internet

1

對於果凍豆(4.1),您可以使用this StackOverflow topic中提供的方法。它將適度地回退到較舊的設備中。 如果您必須回退到等寬字體或襯線字體,請在您使用所選字體的位置聲明文件夾layout-v16,即「sans-serif-condensed」,並在默認文件夾中使用「等寬字體」或「襯線字體」字體。 如果你想退卻到非默認的字體,你可以通過編程檢查Android版本,並選擇適當的行動,即:

if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) { 
    TextView textView = (TextView) findViewById(R.id.textView_id); 
    Typeface myFont = Typeface.createFromAsset(getAssets(),"RobotoCondensed.ttf"); 
    textView.setTypeface(myFont); 
} 
1

這是面向未來的人們遇到同樣的問題,因爲我有。設置字體在加載多行時往往佔用大量內存。一起使用以下兩個代碼實際上使其工作順利。我從stackoverflow得到的解決方案,但他們的答案沒有列在一起。

public class RobotoTextView extends TextView { 

Context context; 

public RobotoTextView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    this.context = context; 
} 

public RobotoTextView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    this.context = context; 
} 

public RobotoTextView(Context context) { 
    super(context); 
    this.context = context; 
} 

public void setTypeface(Typeface tf, int style) { 
    if (!isInEditMode()) { 
     if (style == Typeface.NORMAL) { 
      super.setTypeface(TypeFaceProvider.getTypeFace(getContext(), "fonts/Roboto-Light.ttf")); 
     } else if (style == Typeface.ITALIC) { 
      super.setTypeface(TypeFaceProvider.getTypeFace(getContext(), "fonts/Roboto-LightItalic.ttf")); 
     } else if (style == Typeface.BOLD) { 
      super.setTypeface(TypeFaceProvider.getTypeFace(getContext(), "fonts/Roboto-Bold.ttf")); 
     } else if (style == Typeface.BOLD_ITALIC) { 
      super.setTypeface(TypeFaceProvider.getTypeFace(getContext(), "fonts/Roboto-BoldItalic.ttf")); 
     } 

    } 
} 



public class TypeFaceProvider { 

private static Hashtable<String, Typeface> sTypeFaces = new Hashtable<String, Typeface>(
     4); 

public static Typeface getTypeFace(Context context, String fileName) { 
    Typeface tempTypeface = sTypeFaces.get(fileName); 

    if (tempTypeface == null) { 
     tempTypeface = Typeface.createFromAsset(context.getAssets(), 
       fileName); 
     sTypeFaces.put(fileName, tempTypeface); 
    } 

    return tempTypeface; 
} 
}