2015-04-02 42 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" 
     />