2012-01-19 24 views

回答

18

呀,爲什麼不,你可以得到的Roboto字體:

Android ICS typography

比方說,你想改變文本視圖的字體:

Typeface tf = Typeface.createFromAsset(getAssets(), 
      "fonts/Roboto-Black.ttf"); 
    TextView tv = (TextView) findViewById(R.id.FontTextView); 
    tv.setTypeface(tf); 
+6

有沒有在XML本身設置這種字體屬性的方法?或者更簡單地說,在AndroidManifest.xml本身中使用整個應用程序? – Rahim

+1

我沒有找到任何方法來做到這一點,所以我結束了這個http://stackoverflow.com/questions/9797872/use-roboto-font-for-earlier-devices/10563831#10563831 – Arnaud

-2

您可以在android中應用特定的樣式,允許您更改字體等。查看此鏈接到android開發人員網站。 Styles and Themes

+0

爲什麼這會降低投票率? – tier1

1

要以XML格式設置字體的工作量要稍微多些,但其優點是能夠在XML佈局編輯器的Eclipse ADT的圖形佈局選項卡內預覽字體。再次,首先將您的自定義字體.ttf文件包含在應用程序的資產文件夾中。

創建一個自定義的TextView類:

public class TypefacedTextView extends TextView 
{ 
public TypefacedTextView(Context context, AttributeSet attrs) 
{ 
    super(context, attrs); 

    // Typeface.createFromAsset doesn't work in the layout editor. Skipping ... 
    if (isInEditMode()) 
    { 
    return; 
    } 

    TypedArray styledAttrs = context.obtainStyledAttributes(attrs, R.styleable.TypefacedTextView); 
    String fontName = styledAttrs.getString(R.styleable.TypefacedTextView_typeface); 
    styledAttrs.recycle(); 

    if (fontName != null) 
    { 
    Typeface typeface = Typeface.createFromAsset(context.getAssets(), fontName); 
    setTypeface(typeface); 
    } 
    } 
    } 

我們在您的XML佈局這一習俗TypefacedTextView只需添加您的XML命名空間屬性Android的XML命名空間屬性如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:your_namespace="http://schemas.android.com/apk/res/com.example.app" 
... /> 

和使用您的TypefacedTextView與XML中的普通TextView一樣,但帶有自己的自定義標籤,記住要設置字體:

<com.example.app.TypefacedTextView 
    android:id="@+id/list_item_entry_title" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="center_vertical" 
    android:minHeight="48dp" 
    android:textColor="#FF787878" 
    your_namespace:typeface="Roboto-Regular.ttf" /> 

查看我的博客文章的詳細信息: http://polwarthlimited.com/2013/05/android-typefaces-including-a-custom-font/

+0

404上的鏈接在答案中 – harmanjd

相關問題