2011-03-31 63 views
18

我想改变按钮文字上出现的字体,我已经设法做到这一点与屏幕上的文字,textview,但无法找到任何信息,或帮助将其应用于按钮。更改按钮字体类型的Android帮助,如何?

我是新手,所以提供代码这样做,将不胜感激。这是我用于textview的,但我如何更改按钮字体?

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

感谢 露西 X

回答

47

按钮IS-A TextView,因此只需使用TextView即可:

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

我使用这样的按钮,它的工作(相同的TextView)..

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

希望它可以帮助...

3

如果您计划在同一个字体添加到几个按钮,我建议你去所有的方式,实现它的风格和子键:

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); 
    } 
} 

这是一个辅助类设置字体基于该com.my.package一个TextView(记住,按钮TextView中的一个子类):字体属性:

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); 
     } 
    } 
} 

而这里的FontCache减少对旧设备的内存使用情况:

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; 
    } 
} 

RES /价值/ attrs.xml我们定义自定义设置样式属性

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

最后在布局的例子使用:

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

而在RES /价值/风格.xml

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

这可能看起来像很多工作,但你会感谢我一次y你有几个按钮和文本框,你想改变字体。