2016-08-05 57 views
0

我的自定義字體類自定義字體沒有得到應用

public class CustomFontText extends TextView { 
    /* 
    * Caches typefaces based on their file path and name, so that they don't have to be created every time when they are referenced. 
    */ 
    private static Typeface mTypeface; 

    public CustomFontText(final Context context) { 
     super(context, null); 
    } 

    public CustomFontText(final Context context, final AttributeSet attrs) { 
     super(context, attrs, 0); 
     readAttrs(context, attrs); 
    } 

    public CustomFontText(final Context context, final AttributeSet attrs, final int defStyle) { 
     super(context, attrs, defStyle); 
     readAttrs(context, attrs); 
    } 

    private void readAttrs(Context context, AttributeSet attrs) { 
     TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomTextView); 

     // Read the title and set it if any 
     String fontName = a.getString(R.styleable.CustomTextView_fontname); 
     if (fontName != null) { 
      // We have a attribute value 
      if (mTypeface == null) { 
       mTypeface = Typeface.createFromAsset(context.getAssets(), fontName); 
       setTypeface(mTypeface); 
      } 
     } 

     // a.recycle(); 
    } 
} 

在XML文件中應用

<somepackage.CustomFontText 
      android:id="@+id/details" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="ewfewfewqfewfwef" 
      custom:fontname="Roboto-Regular.ttf" /> 

它不給予任何類型的錯誤,但我不能夠查看TextView的任何改變。更改字體名稱沒有區別。

+0

檢查它鏈接:http://stackoverflow.com/questions/6926263/add-custom-font-for-complete-android-application – prakash

+0

你檢查是否'fontName'不是空?你有資產中的字體嗎? – Blackbelt

+0

@Blackbelt我已將所有字體放在我的資產文件夾中。並且還調試我的代碼以檢查它是否爲空。 – Soham

回答

2

內移動代碼setTypeface(mTypeface);檢查之外mTypeface == null應該解決的問題。因此,代碼應該是這樣的:

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

這是因爲mTypeface聲明static和這樣所有CustomFontText實例共享相同的字體(這是有道理的高速緩存)。如果在檢查內部調用setTypeface,則只會在第一次加載字體時應用一次。

0

其實我不知道你爲什麼不工作,但是,或者你可以使用Calligraphy by chrisjenx。我在我的一個項目中使用它,它很棒!

+0

我不想使用罐庫。有些代碼調整會有幫助 – Soham

0

資產創建文件夾字體項目的文件夾,而不是隻使用字體名稱定製:字體名,使用文件路徑。

custom:fontname="fonts/Roboto-Regular.ttf" 
+0

仍然無法工作): – Soham

0

在資產文件夾中添加ttf或otf文件。

創建自定義類的TextView擴展

public class CustomText extends TextView { 

public CustomText (Context context) { 
    super(context); 
    createTextView(context, null); 
} 

public CustomEditText(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    createTextView(context, attrs); 
} 


public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
    createTextView(context, null); 
} 

private void createTextView(Context context, AttributeSet attrs) { 
    String fontName; 
    TypedArray typedArray; 
    if (isInEditMode()) 
     return; 
    if (attrs != null) { 
     typedArray = context.obtainStyledAttributes(attrs, R.styleable.FontTypeFace, 0, 0); 
     fontName = typedArray.getString(R.styleable.FontTypeFace_typeface); 
     setFontTypeFace(context, fontName); 
     typedArray.recycle(); 
    } 
} 

private void setFontTypeFace(Context context, String fontName) { 
    if (fontName != null) { 
     Typeface typeface = Typeface.createFromAsset(context.getAssets(), fontName); 
     setTypeface(typeface); 
    } 
} 
} 

聲明風格化在ATTRS文件:

<declare-styleable name="FontTypeFace"> 
    <attr name="typeface" format="string" /> 
</declare-styleable> 

在XML文件中使用自定義的TextView創建控制:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:background="@color/white" 
android:orientation="horizontal" 
android:weightSum="1"> 
<com.Widget.CustomTextView 
      android:id="@+id/txt_time" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="@dimen/dimen_text_size_12" 
      app:typeface="@string/thin" /> 
</LinearLayout> 

只需添加資產文件名string.xml文件

<!--String for assets font type file name --> 

<string name="bold">bold.otf</string> 
<string name="light">light.otf</string> 
<string name="medium">medium.otf</string> 
<string name="regular">regular.otf</string> 
<string name="regular_italic">regular_italic.otf</string> 
<string name="semi_bold">semibold.otf</string> 
<string name="thin">thin.otf</string> 
+0

什麼是Custom_TextView_typeface? – Soham