自定义字体没有得到应用

问题描述:

我的自定义字体类自定义字体没有得到应用

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

内移动代码setTypeface(mTypeface);检查之外mTypeface == null应该解决的问题。因此,代码应该是这样的:

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

这是因为mTypeface声明static和这样所有CustomFontText实例共享相同的字体(这是有道理的高速缓存)。如果在检查内部调用setTypeface,则只会在第一次加载字体时应用一次。

其实我不知道你为什么不工作,但是,或者你可以使用Calligraphy by chrisjenx。我在我的一个项目中使用它,它很棒!

+0

我不想使用罐库。有些代码调整会有帮助 – Soham

资产创建文件夹字体项目的文件夹,而不是只使用字体名称定制:字体名,使用文件路径。

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

仍然无法工作): – Soham

在资产文件夹中添加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