2011-03-31 32 views
18

Düğme metninde görünen yazı tipini değiştirmek istiyorum, bunu ekranda metinle yazmayı başarabildim, ancak metin bilgisi bulamıyor, ya da herhangi bir bilgi bulamıyor ya da bunu bir düğmeye uygulamanıza yardımcı oluyor.Android Yardım Düğmesi Yazı Tipi ile Yardım, Nasıl Yapılır?

Acemi oldum, bu yüzden kodun sağlanması çok takdir edilecektir. Bu, metin görünümü için kullanıyorum ama düğme yazı tipini nasıl değiştiririm?

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

Teşekkür Lucy Ben düğmesi için böyle kullanmak x

cevap

47

Düğme IS-A TextView, bu yüzden sadece bir TextView ile yapın:

Eğer tüm yol gidip bir tarz ve alt sınıf düğmesi olarak gerçekleştirmesi ben önermek birkaç düğmelere aynı yazı tipini eklemeyi planlıyorsanız 0
Button txt = (Button) findViewById(R.id.custom_font); 
Typeface font = Typeface.createFromAsset(getAssets(), "1543Humane_jenson_bold.TTF"); 
txt.setTypeface(font); 
6

ve (TextView için aynı) çalıştı ..

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

... Bu yardımcı olur umarım

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

Bu yazı tipini ayarlamak için bir yardımcı sınıftır Font niteliği:

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

Ve burada FontCache eski cihazlarda bellek kullanımını azaltmak için var:

com.my.package dayalı bir TextView (Düğme TextView bir alt sınıfıdır, unutmayın) üzerinde res/values ​​/ attrs.xml biz özel styleable özellikler tanımlamak yılında

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

Ve nihayet bir düzende kullanımına bir örnek verelim:

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

Ve res/değerler/tarzı .xml

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

Bu işin bir çok şey gibi görünebilir, ancak y defada teşekkür edeceğiz yazı tipini değiştirmek istediğiniz birkaç avuç dolusu tuş ve textfields var.