2012-01-27 14 views
24

मैं एक कस्टम फ़ॉन्ट का उपयोग कैसे कर सकता हूं जो मेरे एक्सएमएल में संपत्ति फ़ोल्डर में जोड़ा गया था? मुझे पता है कि हम जावा में setTypeface() विधि का उपयोग कर सकते हैं, लेकिन हमें इसे हर जगह करना है जहां हम TextView का उपयोग करते हैं। तो क्या एक बेहतर तरीका है?एंड्रॉइड एक्सएमएल में कस्टम फ़ॉन्ट का उपयोग कैसे करें?

+0

मैं अपने जवाब को अद्यतन किया है। कृपया उस उत्तर से हटा दें। –

+0

इस ट्यूटोरियल को आजमाएं [http://www.barebonescoder.com/2010/05/android-development-using-custom-fonts/](http://www.barebonescoder.com/2010/05/android-development-using -कस्टम-फोंट /) मुझे लगता है कि यह आपको – Ajay

+0

हाय में मदद करता है हाय कृपया इस पोस्ट को देखें । उसी प्रकृति के साथ एक समस्या पर चर्चा की गई और वहां पर उत्तर दिया गया। – Wajeeh

उत्तर

58

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

package com.vins.test; 

import android.content.Context; 
import android.graphics.Typeface; 
import android.util.AttributeSet; 
import android.widget.TextView; 

public class MyTextView extends TextView { 

    public MyTextView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     init(); 
    } 

    public MyTextView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(); 
    } 

    public MyTextView(Context context) { 
     super(context); 
     init(); 
    } 

    private void init() { 
     Typeface tf = Typeface.createFromAsset(getContext().getAssets(), 
               "your_font.ttf"); 
     setTypeface(tf); 
    } 

} 

हम प्रत्येक लागतकर्ताओं में फ़ॉन्ट सेट करने के लिए init() को कॉल करेंगे। बाद में हमें नीचे दिखाए गए अनुसार हमारे main.xml में इसका उपयोग करना होगा।

<com.vins.test.MyTextView 
    android:id="@+id/txt" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:layout_weight="1" 
    android:text="This is a text view with the font u had set in MyTextView class " 
    android:textSize="30dip" 
    android:textColor="#ff0000" 
    > 

अद्यतन:

पूर्व 4.0 एंड्रॉयड में स्मृति रिसाव के बारे में जागरूक रूप pandre ने उल्लेख किया है।

+0

@vins - क्या होगा यदि उपयोगकर्ता 'एंड्रॉइड' भी कहता है: textStyle = "bold" '? क्या पाठ वास्तव में बोल्ड पर सेट किया जाएगा? –

+0

@ किलाका हां इसे बोल्ड करना चाहिए। – Vins

+1

@ विन्स - धन्यवाद। क्या इसका मतलब यह है कि टीटीएफ फ़ाइल में फ़ॉन्ट के सभी उप-प्रकार शामिल हैं - बोल्ड, इटैलिक, आदि और उनमें से संयोजन? –

2

तो asset\fonts\fontname

में अपने फ़ॉन्ट फ़ाइल रखो अपने xml फ़ाइल में तीन TextView को परिभाषित करें, अपनी गतिविधि कक्षा में इस कोड डाल:

public class AndroidExternalFontsActivity extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     // Font path 
     String fontPath = "fonts/DS-DIGIT.TTF"; 
     String fontPath1 = "fonts/Face Your Fears.ttf"; 
     String fontPath2 = "fonts/HelveticaNeue-Bold_0.otf"; 

     // text view label 
     TextView txtGhost = (TextView) findViewById(R.id.ghost); 
     TextView txtGhost1 = (TextView) findViewById(R.id.ghost1); 
     TextView txtGhost2 = (TextView) findViewById(R.id.ghost2); 

     // Loading Font Face 
     Typeface tf = Typeface.createFromAsset(getAssets(), fontPath); 
     Typeface tf1 = Typeface.createFromAsset(getAssets(), fontPath1); 
     Typeface tf2 = Typeface.createFromAsset(getAssets(), fontPath2); 

     // Applying font 
     txtGhost.setTypeface(tf); 
     txtGhost1.setTypeface(tf1); 
     txtGhost2.setTypeface(tf2); 
    } 
} 
+13

हे फ़ॉन्ट को सेट करने का यह सामान्य तरीका है, सवाल एक्सएमएल में डिफ़ॉल्ट रूप से कस्टम फ़ॉन्ट का उपयोग करने के लिए था। – Vins

संबंधित मुद्दे