2011-03-31 12 views
18

me gustaría cambiar la fuente que aparece en el texto del botón, he logrado hacer esto con el texto en la pantalla, vista de texto, pero no puedo encontrar ninguna información, o ayudo a aplicar esto a un botón.Ayuda de Android con el tipo de fuente cambiante del botón, ¿cómo?

Soy un principiante, por lo que proporcionar el código para hacerlo, sería muy apreciado. Esto es lo que estoy usando para la vista de texto, pero ¿cómo cambio la fuente del botón?

TextView txt = (TextView) findViewById(R.id.custom_font); 
Typeface font = Typeface.createFromAsset(getAssets(), "1543Humane_jenson_bold.TTF"); 
txt.setTypeface(font); 

Gracias Lucy x

Respuesta

47

Botón IS-A TextView, así que hazlo como con un TextView:

Button txt = (Button) findViewById(R.id.custom_font); 
Typeface font = Typeface.createFromAsset(getAssets(), "1543Humane_jenson_bold.TTF"); 
txt.setTypeface(font); 
6

utilizo como esto para el botón y funcionó (lo mismo que para la Vista de Texto) ..

Button enter=(Button) findViewById(R.id.enter); 
Typeface type=Typeface.createFromAsset(getAssets(), "arial.ttf"); 
enter.setTypeface(type); 

creo que sirve ...

3

Si va a añadir la misma fuente a varios botones le sugiero que ir hasta el final e implementarlo como un estilo y subclase botón:

public class ButtonPlus extends Button { 

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

    public ButtonPlus(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     CustomFontHelper.setCustomFont(this, context, attrs); 
    } 

    public ButtonPlus(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     CustomFontHelper.setCustomFont(this, context, attrs); 
    } 
} 

Ésta es una clase de ayuda a establecer una fuente en una TextView (recuerda, Button es una subclase de Vista de Texto) basado en el com.my.package: fuente de atributos:

public class CustomFontHelper { 

    /** 
    * Sets a font on a textview based on the custom com.my.package:font attribute 
    * If the custom font attribute isn't found in the attributes nothing happens 
    * @param textview 
    * @param context 
    * @param attrs 
    */ 
    public static void setCustomFont(TextView textview, Context context, AttributeSet attrs) { 
     TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomFont); 
     String font = a.getString(R.styleable.CustomFont_font); 
     setCustomFont(textview, font, context); 
     a.recycle(); 
    } 

    /** 
    * Sets a font on a textview 
    * @param textview 
    * @param font 
    * @param context 
    */ 
    public static void setCustomFont(TextView textview, String font, Context context) { 
     if(font == null) { 
      return; 
     } 
     Typeface tf = FontCache.get(font, context); 
     if(tf != null) { 
      textview.setTypeface(tf); 
     } 
    } 
} 

Y es el fontcache para reducir el uso de memoria en los dispositivos más antiguos aquí:

public class FontCache { 

    private static Hashtable<String, Typeface> fontCache = new Hashtable<String, Typeface>(); 

    public static Typeface get(String name, Context context) { 
     Typeface tf = fontCache.get(name); 
     if(tf == null) { 
      try { 
       tf = Typeface.createFromAsset(context.getAssets(), name); 
      } 
      catch (Exception e) { 
       return null; 
      } 
      fontCache.put(name, tf); 
     } 
     return tf; 
    } 
} 

En res/valores/attrs.xml que definen el atributo personalizado styleable

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="CustomFont"> 
     <attr name="font" format="string"/> 
    </declare-styleable> 
</resources> 

Y, finalmente, un ejemplo de uso de un diseño:

<com.my.package.buttons.ButtonPlus 
    style="@style/button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/button_sometext"/> 

Y en res/valores/estilo .xml

<style name="button" parent="@android:style/Widget.Button"> 
    <item name="com.my.package:font">fonts/copperplate_gothic_light.TTF</item> 
</style> 

Esto puede parecer mucho trabajo, pero me lo agradecerán una vez Tiene dos puñados de botones y campos de texto en los que desea cambiar la fuente.

Cuestiones relacionadas