2016-08-05 18 views
0

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.

+0

überprüfen Sie diesen Link: http: //stackoverflow.com/questions/6926263/add-custom-font-for-complete-android-application – prakash

+0

haben Sie überprüft, ob 'fontName' nicht null ist? Haben Sie die Schriftarten in den Assets? – Blackbelt

+0

@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

Antwort

2

den Code setTypeface(mTypeface); außerhalb der Kontrolle der mTypeface == null Umzug sollte das Problem lösen. So sollte der Code wie folgt aussehen:

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

Dies liegt daran, mTypefacestatic deklariert und so alle CustomFontText Instanzen teilen sich die gleiche Schrift (die Sinn für das Caching macht). Wenn setTypeface innerhalb der Prüfung aufgerufen wird, wird es nur einmal angewendet, wenn die Schriftart zum ersten Mal geladen wird.

0

Eigentlich weiß ich nicht, warum Ihre nicht funktioniert, alternativ können Sie Calligraphy by chrisjenx verwenden. Ich habe es in einem meiner Projekte verwendet und es funktioniert großartig!

+0

Ich möchte keine Bibliotheken verwenden. Einige Code-Optimierungen wären hilfreich – Soham

0

Ordner erstellen Schriftarten in den Vermögen Ordner Ihres Projekts und stattdessen nur den Namen der Schriftart für Brauch mit: Fontname, den Pfad zur Datei.

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

funktioniert immer noch nicht): – Soham

0

Fügen Sie die ttf- oder otf-Datei in Ihren Assets-Ordner ein.

Erstellen von benutzerdefinierten Klasse erweitert mit 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); 
    } 
} 
} 

declare frisierbar in Ihrer attrs Datei:

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

Kontrolle Erstellen von benutzerdefinierten Textview in xml-Datei mit:

<?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> 

Fügen Sie einfach die Vermögenswerte Dateinamen innerhalb string.xml Datei

<!--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

Was ist Custom_TextView_typeface? – Soham

Verwandte Themen