我的自定義字體類自定義字體沒有得到應用
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的任何改變。更改字體名稱沒有區別。
檢查它鏈接:http://stackoverflow.com/questions/6926263/add-custom-font-for-complete-android-application – prakash
你檢查是否'fontName'不是空?你有資產中的字體嗎? – Blackbelt
@Blackbelt我已將所有字體放在我的資產文件夾中。並且還調試我的代碼以檢查它是否爲空。 – Soham