2016-11-13 30 views
0

我已经得到了很多,在我的片段“正常的”自定义字体的问题,所以我决定从本教程让一切:扩展TextView中的自定义字体? Android的

https://futurestud.io/tutorials/custom-fonts-on-android-extending-textview

,并没有工作。我没有收到任何错误。请帮帮我!

+1

这是因为它正在迎头赶上为你报错。它说在FontCache.java中捕获,替换返回null与Log.e(“失败”,e.getMessage());然后发布你的错误。 – BIW

回答

0
public class LoveTextView extends TextView { 
     private static final String TAG = "TextView"; 

     public LoveTextView(Context context) { 
      super(context); 
     } 

     public LoveTextView(Context context, AttributeSet attrs) { 
      super(context, attrs); 
      setCustomFont(context, attrs); 
     } 

     public LoveTextView(Context context, AttributeSet attrs, int defStyle) { 
      super(context, attrs, defStyle); 
      setCustomFont(context, attrs); 
     } 

     private void setCustomFont(Context ctx, AttributeSet attrs) { 
      TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.LoveTextView); 
      String customFont = a.getString(R.styleable.LoveTextView_customFont); 
      setCustomFont(ctx, customFont); 
      a.recycle(); 
     } 

     public boolean setCustomFont(Context ctx, String asset) { 
      Typeface tf = null; 
      try { 
       tf = Typeface.createFromAsset(ctx.getAssets(), asset); 
      } catch (Exception e) { 
       Log.e(TAG, "Could not get typeface: " + e.getMessage()); 
       return false; 
      } 

      setTypeface(tf); 
      return true; 
     } 

    } 




than you can use textview class in xml file . like below 

<com.love.ui.LoveTextView 
      android:id="@+id/toolbar_title" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="left|center" 
      android:text="@string/add_product" 
      android:textColor="@color/title_text_color" 
      android:textSize="@dimen/material_medium_one" 
      app:customFont="fonts/Roboto-Regular.ttf" />