2015-08-05 1 views
0

J'ai besoin de définir le style du texte pour textview selon l'image ci-dessousAVC dans le texte textview android?

TEXT VIEW FONT

La police utilisée est CARTER une police

C'est ma classe TextView

public class CustomTextViewCarter extends TextView { 
private static Typeface mTypeface; 
public CustomTextViewCarter(final Context context) { 
    this(context, null); 
} 

public CustomTextViewCarter(final Context context, final AttributeSet attrs) { 
    this(context, attrs, 0); 

} 

public CustomTextViewCarter(final Context context, final AttributeSet attrs, final int defStyle) { 
    super(context, attrs, defStyle); 
    if (!isInEditMode()) { 
     if (mTypeface == null) { 
      mTypeface = Typeface.createFromAsset(context.getResources().getAssets(), "carter_one.ttf"); 
     } 
     setTypeface(mTypeface); 
    } 
} 

}

Mais le même type de texte n'apparaît pas. Comment ceci peut être fait?

+4

vous pouvez utiliser la bibliothèque ** https: //github.com/chrisjenx/Calligraphy** –

Répondre

0

Essayez ceci. Vous pouvez définir ttf en créant votre propre implémentation de textview. Il travaillera au-dessus de l'API 15 (sandwich à la crème glacée).

https://stackoverflow.com/a/5185587/850347

<com.lht.ui.MyTextView 
    android:text="Hello friends" 
    lht:ttf_name="ITCBLKAD.TTF" 
    /> 
1

Utilisez ce code pour le spectacle TextView personnalisé étape 1: CustomTextView.java

public class CustomTextView extends TextView { 
    private static final String TAG = "TextView"; 

    public CustomTextView(Context context) { 
     super(context); 
    } 

    public CustomTextView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     setCustomFont(context, attrs); 
    } 

    public CustomTextView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     setCustomFont(context, attrs); 
    } 

    private void setCustomFont(Context ctx, AttributeSet attrs) { 
     TypedArray a = ctx.obtainStyledAttributes(attrs,R.styleable.CustomTV); 
     String customFont = a.getString(R.styleable.CustomTV_customFont); 
     setCustomFont(ctx, customFont); 
     a.recycle(); 
    } 

    public boolean setCustomFont(Context ctx, String asset) { 
     Typeface tf = null; 
     try { 
     tf = Typeface.createFromAsset(ctx.getAssets(), asset); 
     } catch (Exception e) { 
      Log.e(TAG, "Could not get typeface: "+e.getMessage()); 
      return false; 
     } 

     setTypeface(tf); 
     return true; 
    } 

} 

Étape 2: Après cela écrire ci-dessous code dans votre style s.xml fichier

<declare-styleable name="CustomTV"> 
     <attr name="customFont" format="string"></attr> 
</declare-styleable> 

Étape 3: Utilisez CustomTextView dans votre fichier xml Ici com.app.demo est mon nom de package .. utiliser votre nom de package au lieu de cela,

<com.app.demo.CustomTextView 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/title" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="hello" 
    android:textColor="@color/text" 
    android:textStyle="bold" 
    app:customFont="carter_one.ttf" />