My Custom Font-KlasseBenutzerdefinierte Schriftart nicht
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();
}
}
Anwendung in XML-Datei
<somepackage.CustomFontText
android:id="@+id/details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ewfewfewqfewfwef"
custom:fontname="Roboto-Regular.ttf" />
geben Es ist nicht jede Art von Fehler angewendet zu werden, aber ich bin nicht in der Lage alle Änderungen in der Textview zu sehen . Das Ändern des Fontnamens macht keinen Unterschied.
überprüfen Sie diesen Link: http: //stackoverflow.com/questions/6926263/add-custom-font-for-complete-android-application – prakash
haben Sie überprüft, ob 'fontName' nicht null ist? Haben Sie die Schriftarten in den Assets? – Blackbelt
@Blackbelt Ich habe alle Fonts in meinen Asset-Ordner gelegt. Und auch debugge ich meinen Code, um zu überprüfen, ob es null ist oder nicht. – Soham