2015-04-02 41 views

回答

0

不,您不能在佈局文件(XML)中使用自定義字體。您必須以編程方式使用自定義字體,如下所示:

String fontPath = "fonts/calibrib.ttf"; 

      // Loading Font Face 
      Typeface tf = Typeface.createFromAsset(mContext.getAssets(), fontPath); 

TextView txtView = new TextView(this); 
txtView.setTypeface(tf); 

將您的字體放置在資產文件夾中。這裏是上面的代碼工作。

enter image description here

0

你可以通過重寫這樣TextView的創建自己的TextView,你必須把字體的資產文件夾:

public class MyTextView extends TextView { 

    public MyTextView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     setType(context); 
    } 

    public MyTextView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     setType(context); 
    } 

    public MyTextView(Context context) { 
     super(context); 
     setType(context); 
    } 

    private void setType(Context context){ 
     this.setTypeface(Typeface.createFromAsset(context.getAssets(), 
        "foo.ttf")); 

     this.setShadowLayer(1.5f, 5, 5, getContext().getResources().getColor(R.color.black_shadow)); 
    } 
} 

而且使用這樣的:

<com.your.project.package.MyTextView 
     android:id="@+id/oppinfo_mtv_name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom" 
     android:text="Player 1" 
     />