2012-02-28 23 views
23

मैं एक कस्टम टेक्स्ट दृश्य बनाने की कोशिश कर रहा हूं जिसमें दिए गए पथ से फ़ॉन्ट सेट है। मुझे किसी भी उदाहरण देते हैं और कैसे मैं कम कोड के साथ कि कर सकते हैं करें:कस्टम टेक्स्ट व्यू कैसे बनाएं?

<TextView 
    android:id="@+id/textView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/accountInfoText" 
    android:textColor="#727272" 
    android:textSize="18dp" /> 
+1

देखें [इस] (http://developer.android.com/guide/topics/ui/themes.html) से बचने के लिए – Ghost

उत्तर

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

public class FontTextView extends TextView { 


   public FontTextView(Context context) { 
     super(context); 
     Typeface face=Typeface.createFromAsset(context.getAssets(), "Helvetica_Neue.ttf"); 
     this.setTypeface(face); 
   } 

   public FontTextView(Context context, AttributeSet attrs) { 
       super(context, attrs); 
    Typeface face=Typeface.createFromAsset(context.getAssets(), "Helvetica_Neue.ttf"); 
 this.setTypeface(face); 
   } 

   public FontTextView(Context context, AttributeSet attrs, int defStyle) { 
       super(context, attrs, defStyle); 
    Typeface face=Typeface.createFromAsset(context.getAssets(), "Helvetica_Neue.ttf"); 
 this.setTypeface(face); 
   } 

   protected void onDraw (Canvas canvas) { 
       super.onDraw(canvas); 
        
      
   } 

} 

व एक्सएमएल में:

<com.util.FontTextView 
                   android:id="@+id/textView2" 
                   android:layout_width="wrap_content" 
                   android:layout_height="wrap_content" 
                   android:text="@string/accountInfoText" 
                   android:textColor="#727272" 
                   android:textSize="18dp" /> 
+28

मैं फ़ॉन्ट लागू करने के लिए एक सिंगलटन का उपयोग करना चाहिये प्रत्येक बार संपत्ति से टाइपफेस बनाना। आपकी बहुत ही उपयोगी टिप्पणी के लिए – JackMahoney

+0

@JackMahoney +1। अब मेरा आवेदन बहुत तेज है !! –

+0

@ जैकमोहनी क्या आपके पास एक कस्टम व्यू को सिंगलटन में दिखाने के लिए कनवर्ट करने का एक उदाहरण है? सहायक होगा। – kabuto178

-1

नीचे कस्टम वर्ग अपने आवश्यक फ़ॉन्ट स्थापित करने के लिए अनुकूलित करने के लिए मदद कर सकता है TextView पर आपको संपत्ति में कुछ .ttf फ़ाइल डालना होगा और यह पथ कस्टम TextView पर देना होगा।

public class TextViewBoldFont extends TextView { 
    public TextViewBoldFont(Context context, AttributeSet attrs) { 
     super(context, attrs); 

     String fontPath = "GOTHICB.TTF"; 
     Typeface fontsStyle = Typeface.createFromAsset(context.getAssets(), fontPath); 
     this.setTypeface(fontsStyle,Typeface.BOLD); 
    } 

    public TextViewBoldFont(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 

     String fontPath = "GOTHICB.TTF"; 
     Typeface fontsStyle = Typeface.createFromAsset(context.getAssets(), fontPath); 
     this.setTypeface(fontsStyle,Typeface.BOLD); 
    } 

    public TextViewBoldFont(Context context) { 
     super(context); 

     String fontPath = "GOTHICB.TTF"; 
     Typeface fontsStyle = Typeface.createFromAsset(context.getAssets(), fontPath); 
     this.setTypeface(fontsStyle,Typeface.BOLD); 
    } 
} 
+2

हाय, एसओ में आपका स्वागत है। कृपया कोड को उत्तर के रूप में डंप न करें, विचार की अपनी ट्रेन को समझाएं ताकि उपयोगकर्ता समझ सकें कि आप क्या कर रहे हैं। धन्यवाद। स्वीकृत उत्तर की तुलना में – Cthulhu

18

Textview के लिए कस्टम व्यू बनाएं। चरण 1 - attrs.xml फ़ाइल में प्रवेश करें और कस्टम TextView में फ़ॉन्ट के रूप में फ़ॉन्ट का चयन करने का विकल्प दें।

<declare-styleable name="CustomFontTextView"> 
    <attr name="fontName"/> 
</declare-styleable> 

चरण 2: फोंट की सूची के साथ enum प्रविष्टि बनाएँ और विशिष्ट मानों

<attr name="fontName" format="enum"> 
    <enum name="Roboto_Bold" value="1" /> 
    <enum name="Roboto_Italic" value="2" /> 
    <enum name="Roboto_Light" value="3" /> 
    <enum name="Roboto_Medium" value="4" /> 
    <enum name="Roboto_Regular" value="5" /> 
    <enum name="Roboto_Thin" value="6" /> 
</attr> 

चरण 3 में निर्दिष्ट करें: में strings.xml

<string name="Roboto_Bold">Roboto-Bold</string> 
<string name="Roboto_Medium">Roboto-Medium</string> 
<string name="Roboto_Light">Roboto-Light</string> 
<string name="Roboto_Regular">Roboto-Regular</string> 
<string name="Roboto_Thin">Roboto-Thin</string> 
<string name="Roboto_Italic">Roboto-Italic</string> 

चरण 4 सभी फ़ॉन्ट की प्रविष्टियों करें: बनाने संपत्ति फ़ोल्डर और सभी आवश्यक फ़ॉन्ट कॉपी करें जिन्हें आप फ़ॉन्ट फ़ोल्डर में रखना चाहते हैं

चरण 5: एक क्लै एसएस TextView

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

/** 
* Created by ANKIT 
*/ 
public class CustomFontTextView extends TextView { 

    String customFont; 

    public CustomFontTextView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     style(context, attrs); 
    } 

    public CustomFontTextView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     style(context, attrs); 

    } 

    private void style(Context context, AttributeSet attrs) { 

     TypedArray a = context.obtainStyledAttributes(attrs, 
       R.styleable.CustomFontTextView); 
     int cf = a.getInteger(R.styleable.CustomFontTextView_fontName, 0); 
     int fontName = 0; 
     switch (cf) 
     { 
      case 1: 
       fontName = R.string.Roboto_Bold; 
       break; 
      case 2: 
       fontName = R.string.Roboto_Italic; 
       break; 
      case 3: 
       fontName = R.string.Roboto_Light; 
       break; 
      case 4: 
       fontName = R.string.Roboto_Medium; 
       break; 
      case 5: 
       fontName = R.string.Roboto_Regular; 
       break; 
      case 6: 
       fontName = R.string.Roboto_Thin; 
       break; 
      default: 
       fontName = R.string.Roboto_Regular; 
       break; 
     } 

     customFont = getResources().getString(fontName); 

     Typeface tf = Typeface.createFromAsset(context.getAssets(), 
       "font/" + customFont + ".ttf"); 
     setTypeface(tf); 
     a.recycle(); 
    } 
} 

आप इस कस्टम क्लास का इस तरह से उपयोग कर सकते हैं। .. का उपयोग अपने packageName.ClassName

<ankit.com.customui.CustomFontTextView 
    android:layout_width="match_parent" 
    android:text="Hello World Ankit" 
    android:textSize="16sp" 
    app:fontName="Roboto_Medium" 
    android:layout_height="wrap_content"/> 
+3

बहुत बेहतर कार्यान्वयन। –

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