2011-03-31 38 views
18

मैं बटन टेक्स्ट पर दिखाई देने वाले फ़ॉन्ट को बदलना चाहता हूं, मैंने स्क्रीन पर टेक्स्ट के साथ ऐसा करने में कामयाब रहा है, टेक्स्टव्यू, लेकिन कोई जानकारी नहीं मिल पा रही है, या बटन पर इसे लागू करने में मदद कर सकती है।बदलते बटन फ़ॉन्ट प्रकार के साथ एंड्रॉइड सहायता, कैसे?

मैं नौसिखिया हूं, इसलिए ऐसा करने के लिए कोड प्रदान करना बहुत सराहना की जाएगी। मैं टेक्स्टव्यू के लिए उपयोग कर रहा हूं, लेकिन मैं बटन फ़ॉन्ट कैसे बदलूं?

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

धन्यवाद लुसी एक्स

उत्तर

47

बटन IS-A टेक्स्ट व्यू, तो बस इसे टेक्स्टव्यू के साथ करें:

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

यह एक फ़ॉन्ट स्थापित करने के लिए एक सहायक वर्ग है फ़ॉन्ट विशेषता:

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 पुराने उपकरणों पर स्मृति उपयोग को कम करने के तरीके:

एक TextView (याद रखें, बटन TextView का एक उपवर्ग है) com.my.package के आधार पर पर

में res/values ​​/ attrs.xml हम कस्टम Styleable विशेषता परिभाषित

<?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/values ​​/ शैली में .xml

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

यह एक बहुत सारे काम की तरह प्रतीत हो सकता है, लेकिन आप एक बार मुझे धन्यवाद देंगे कहां बटन और टेक्स्टफील्ड के कुछ मुट्ठी भर हैं जिन्हें आप फ़ॉन्ट बदलना चाहते हैं।