2011-08-03 32 views
32

मेरे आवेदन में मुझे सभी टेक्स्टव्यू और संपादन-टेक्स्ट फ़ील्ड के लिए हेल्वैटिका फ़ॉन्ट का उपयोग करने की आवश्यकता है। क्या प्रत्येक पाठदृश्य के लिए settypeface विधि का उपयोग करने के अलावा ऐसा करने का कोई तरीका है? कोई भी सुझाव एक बड़ी मदद होगी।पूर्ण एंड्रॉइड एप्लिकेशन के लिए कस्टम फ़ॉन्ट जोड़ें

अग्रिम धन्यवाद!

+0

जाएँ इस एक: हो सकता है कि यह आप के लिए उपयोगी हो सकते हैं: http://stackoverflow.com/questions/2888508/कैसे-टू-चेंज-द-फ़ॉन्ट-ऑन-द-टेक्स्टव्यू/16166184 # 16166184 –

+0

नहीं, कोई रास्ता नहीं है। आपको अपने फ़ॉन्ट को बंडल करना होगा और जैसा कि निक कहते हैं। आप इस धागे को भी देख सकते हैं [http://stackoverflow.com/questions/2376250/custom-fonts-and-xml-layouts-android ](httpoverstow.com/questions/2376250/custom-fonts-and -xml-layouts-android) –

+0

किसी को भी एसी #/एक्समरिन समाधान की आवश्यकता है जो टेक्स्टव्यू बढ़ाता है लेकिन यह सामान्य बनाता है यानी आप xml पर प्रत्येक टेक्स्टव्यू पर एक विशेषता के रूप में एक विशिष्ट फ़ॉन्ट पथ पारित कर सकते हैं, फिर यहां एक नज़र डालें https: //github.com/Cheesebaron/Cheesebaron.FontSample –

उत्तर

63

मैंने इसे अपने स्वयं से समझ लिया। यह वह कोड है जिसका मैंने उपयोग किया था। मैं कस्टम 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(), "font/chiller.ttf"); 
     setTypeface(tf ,1); 

    } 

} 
+0

आप "टेक्स्ट व्यू" का विस्तार करने के लिए उत्सुक नहीं हैं, लेकिन टिप – mxg

+2

@mxg के लिए धन्यवाद, मैंने जो देखा है उसे नहीं मिला – Chrishan

+9

@mxg टेक्स्टव्यू ओवरराइड करके वह अपनी लेआउट फ़ाइलों में MyTextView का उपयोग करने में सक्षम है बिना प्रोग्रामेटिक रूप से कुछ भी कस्टम फ़ॉन्ट का एहसास करने के लिए गतिविधि/खंड। –

9

एक शैली बनाएं और इसे सभी टेक्स्ट विशेषताओं का उपयोग करें।

<style name="CustomText"> 
    <item name="android:typeface">YourFontName</item> 
</style> 

यह प्रयोग करें:

<TextView style="@style/CustomText" /> 

ऊपर अनुकूलन आप उपयोग कर सकते हैं के लिए सभी गतिविधि में कस्टम फ़ॉन्ट का उपयोग करने के लिए है ....

Typeface font = Typeface.createFromAsset(getAssets(), "CustomFontName.ttf"); 
txt.setTypeface(font); 

कोशिश यह।

+11

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

+0

@Chrishi देखें कि मैंने जवाब संपादित किया है। – Hanry

+0

+1 आपका उत्तर मुझे बहुत मदद करता है @hanry –

0

ठीक है आप इसे कर सकते हैं लेकिन आप जो भी अनिवार्य रूप से बनाना चाहते हैं वह एक प्रकार के मानचित्र प्रारूप में एक फ़ॉन्ट एटलस है (इसे यूनिकोड ऑर्डर में चलाना होगा जो शुरू होता है! "# $% '() X +, -। /) कार्य क्या करेगा, एक स्ट्रिंग में ले जाएगा और काम करता है जहां एटलस में प्रत्येक संबंधित अक्षर होता है।

यह बहुत आसान नहीं है लेकिन प्रत्येक चरित्र की लंबाई और चौड़ाई होनी चाहिए, लेकिन आपके पास अलग-अलग लंबाई हो सकती है लेकिन वह एक बहुत कठिन हो।

27

अपनी गतिविधियों में, सही होने के बाद आप कॉल

setContentView (री d.blahblah);

आपको विगेट्स के पूरे पदानुक्रम पर जाने और फ़ॉन्ट प्रतिस्थापन से निपटने के लिए एक विधि चलनी चाहिए;

setContentView(R.id.blahblah); 
Utils.overrideFonts(this, findViewById(android.R.id.content)); 

और उल्लिखित "ओवरराइड फ़ॉन्ट्स" विधि कुछ ऐसा होना चाहिए;

public static void overrideFonts(final Context context, final View v) { 
    try { 
     if (v instanceof ViewGroup) { 
      ViewGroup vg = (ViewGroup) v; 
      for (int i = 0; i < vg.getChildCount(); i++) { 
       View child = vg.getChildAt(i); 
       overrideFonts(context, child); 
      } 
     } else if (v instanceof TextView) { 
      ((TextView)v).setTypeface(FONT_REGULAR); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
     // ignore 
    } 
} 

इस योजना में FONT_REGULAR कहीं सुरक्षित रूप से प्रारंभ किया जाना चाहिए, यदि आप एक सिंगलटन या किसी अन्य तरीके यकीन है कि यह ठीक से प्रारंभ कर रहा है होने की कल्पना कर सकते हैं ...

private static void initializeFonts(final Context context) { 
    FONT_REGULAR = Typeface.createFromAsset(context.getAssets(), "fonts/myfont_medium.otf"); 
    FONT_BOLD = Typeface.createFromAsset(context.getAssets(), "fonts/myfont_bold.otf"); 
} 

आप एक उपवर्ग का उपयोग करते हैं MyAppActivity (गतिविधि को बढ़ाता है) जैसी गतिविधि, तो आपको इस तरह के अनुकूलन के लिए प्रत्येक गतिविधि वर्ग को बदलने की आवश्यकता नहीं है। इसके बजाय आप इसमें कटौती कर सकते हैं और व्यवहार को ओवरराइड कर सकते हैं;

public class MyAppActivity extends Activity { 
... ... 
    @Override 
    public void setContentView(final int layoutResID) { 
     super.setContentView(layoutResID); 
     Utils.overrideFonts(this, findViewById(android.R.id.content)); 
    } 
... ... 
} 

इस तरह आप सामान्य व्यवहार के लिए अपनी किसी भी गतिविधि का उपयोग कर सकते हैं;

public class SettingsUI extends MyAppActivity { 
... ... 
} 

मुझे उम्मीद है कि यह मदद करता है ... चीयर्स!

+6

यह सरल लेआउट के लिए एक अच्छा समाधान है। हालांकि, अगर आपके पास अपनी गतिविधि में लिस्ट व्यू है, तो यह काम नहीं करेगा। ListView में आइटम सेटकंटेंट विधि के बाद तत्काल होते हैं और इसलिए, सूची में टेक्स्टव्यू इस विधि से प्रभावित नहीं होगा – Maggie

+0

आप सही हैं, इन्हें संभालने की आवश्यकता है ... ItemAdapter क्लास – lithium

+2

एक्शनबार के फ़ॉन्ट के बारे में क्या? –

0

आप http://bit.ly/1bzjWQn

पर PixlUI उपयोग कर सकते हैं अपनी परियोजना में उनके .jar आयात करते हैं। एक्सएमएल में इसका इस्तेमाल

<com.neopixl.pixlui.components.textview.TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/hello_world" 
    pixlui:typeface="MyFont.ttf" /> 

भूल मत योजना को लागू (xmlns: pixlui = "http://schemas.android.com/apk/com.neopixl.pixlui")

+1

हमें कुछ जार जोड़ने की आवश्यकता क्यों है जो हम पूरी तरह से एंड्रॉइड के साथ कर सकते हैं? – Chrishan

+0

आप एंड्रॉइड एसडीके के साथ कस्टम फोंट का उपयोग नहीं कर सकते हैं – odemolliens

6

कस्टम फ़ॉन्ट लागू करने के लिए

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    FontManager.getInstance().initialize(this, R.xml.fonts); 
    setContentView(R.layout.main); 
} 
@Override 
public View onCreateView(String name, Context context, AttributeSet attrs) { 
    SimpleFactory factory = new SimpleFactory(); 
    return factory.onCreateView(name, context, attrs); 
} 

जहां FontManager इस वर्ग कि सभी फ़ॉन्ट्स कि /xml/fonts.xml और SimpleFactory सिर्फ कारखाना है कि दृश्य बनाने और प्रत्येक के लिए कस्टम फ़ॉन्ट लागू में परिभाषित का प्रबंधन कि उदाहरण देखने जाता है: सभी आवेदन भर में, बस नीचे दिए गतिविधि बनाने पाठ दृश्य का।

/xml/fonts.xml

<?xml version="1.0" encoding="utf-8"?> 
<familyset> 
    <family> 
     <nameset> 
      <!--Font name--> 
      <name>HelveticaNeueLTStd</name> 
     </nameset> 
     <fileset> 
      <!--Font styles--> 
      <file style="normal">fonts/HelveticaNeueLTStd-LtCn.otf</file> 
      <file style="bold">fonts/HelveticaNeueLTStd-MdCn.otf</file> 
      <file style="italic">fonts/HelveticaNeueLTStd-LtCnO.otf</file> 
      <file style="bold_italic">fonts/HelveticaNeueLTStd-MdCnO.otf</file> 
     </fileset> 
    </family> 
    <family> 
     <!--There new font family can be added, 
don't forget add font files into /assets/fonts directory and 
put the name of the font into /values/string/font.xml--> 
    </family> 
</familyset> 

FontFactory - बस/xml/फोंट में परिभाषित फ़ॉन्ट फ़ाइलों को मैप - abastract वर्ग, बस अपने खुद के कारखाने

public abstract class FontFactory implements LayoutInflater.Factory{ 
    public final String TAG = getClass().getSimpleName(); 

    static final Class<?>[] mConstructorSignature = new Class[] {Context.class, AttributeSet.class}; 
    final Object[] mConstructorArgs = new Object[2]; 
    private static final String[] sClassPrefixList = { 
      "android.widget.", 
      "android.webkit." 
    }; 

    @Override 
    public View onCreateView(String name, Context context, AttributeSet attrs) { 
     if("ViewStub".equals(name) || "View".equals(name)){ 
      return null; 
     } 
     View view = null; 
     Constructor<? extends View> constructor = null; 
     Class clazz = null; 

     if (view == null) { 
      if (-1 == name.indexOf('.')) { 
       for (String prefix : sClassPrefixList) { 
        clazz = getClazz(prefix, name); 
        if(clazz != null){ 
         break; 
        } 
       } 
      } else { 
       clazz = getClazz("", name); 
      } 
     } 

     if (clazz == null) { 
      Log.d(TAG, "View can't be created " + name); 
      return null; 
     } 

     try { 
      constructor = clazz.getConstructor(mConstructorSignature); 
     } catch (NoSuchMethodException e) { 
      e.printStackTrace(); 
     } 

     Object[] args = mConstructorArgs; 
     args[1] = attrs; 

     if(constructor == null){ 
      return null; 
     } 

     try { 
       view = constructor.newInstance(context, attrs); 
     } catch (InstantiationException e) { 
      e.printStackTrace(); 
     } catch (IllegalAccessException e) { 
      e.printStackTrace(); 
     } catch (InvocationTargetException e) { 
      e.printStackTrace(); 
     } 

     if(view != null){ 
      onFontApply(context, view); 
     } 
     return view; 
    } 

    public abstract void onFontApply(Context context, View view); 

    private Class getClazz(String prefix, String name){ 
     Class clazz = null; 
     try { 
      clazz = Class.forName(prefix + name); 
     } catch (ClassNotFoundException e) { 
      e.printStackTrace(); 
     } finally { 
      return clazz; 
     } 
    } 
} 

FontManager बनाने के लिए इसका फैली हुई है। एक्सएमएल फ़ॉन्ट फ़ाइलों को/संपत्ति निर्देशिका में रखा गया है और फ़ॉन्ट परिवार नाम और फ़ॉन्ट शैली द्वारा टाइपफेस लौटाएं।

public void initialize(Context context, int resId) { 
    if(mFonts != null){ 
     Log.d(TAG,"FontManager have already initialized"); 
     return; 
    } 
    XmlResourceParser parser = null; 
    try { 
     parser = context.getResources().getXml(resId); 
     mFonts = new ArrayList<Font>(); 

     String tag; 
     String fontStryleAttr = null; 
     int eventType = parser.getEventType(); 

     Font font = null; 

     do { 
      tag = parser.getName(); 

      switch (eventType) { 
       case XmlPullParser.START_TAG: 
        if (tag.equals(TAG_FAMILY)) { 
         // one of the font-families. 
         font = new Font(); 
        } else if (tag.equals(TAG_NAMESET)) { 
         // a list of font-family names supported. 
         font.families = new ArrayList<String>(); 
        } else if (tag.equals(TAG_NAME)) { 
         isName = true; 
        } else if (tag.equals(TAG_FILESET)) { 
         // a list of files specifying the different styles. 
         font.styles = new ArrayList<FontStyle>(); 
        } else if (tag.equals(TAG_FILE)) { 
         isFile = true; 
         fontStryleAttr = parser.getAttributeValue(null, ATTR_STYLE); 
        } 
        break; 

       case XmlPullParser.END_TAG: 
        if (tag.equals(TAG_FAMILY)) { 
         // add it to the list. 
         if (font != null) { 
          mFonts.add(font); 
          font = null; 
         } 
        } else if (tag.equals(TAG_NAME)) { 
         isName = false; 
        } else if (tag.equals(TAG_FILE)) { 
         isFile = false; 
         fontStryleAttr = null; 
        } 
        break; 

       case XmlPullParser.TEXT: 
        String text = parser.getText(); 
        if (isName) { 
         // value is a name, add it to list of family-names. 
         if (font.families != null) 
          font.families.add(text); 
        } else if (isFile) { 
         // value is a file, add it to the proper kind. 
         FontStyle fontStyle = new FontStyle(); 
         fontStyle.font = Typeface.createFromAsset(context.getAssets(), text); 
         String attr = parser.getAttributeValue(null, ATTR_STYLE); 
         if (fontStryleAttr.equals(STYLE_BOLD)) 
          fontStyle.style = Typeface.BOLD; 
         else if (fontStryleAttr.equals(STYLE_ITALIC)) 
          fontStyle.style = Typeface.ITALIC; 
         else if (fontStryleAttr.equals(STYLE_BOLD_ITALIC)) 
          fontStyle.style = Typeface.BOLD_ITALIC; 
         else 
          fontStyle.style = Typeface.NORMAL; 
         font.styles.add(fontStyle); 
        } 
      } 

      eventType = parser.next(); 

     } while (eventType != XmlPullParser.END_DOCUMENT); 

    } catch (XmlPullParserException e) { 
     throw new InflateException("Error inflating font XML", e); 
    } catch (IOException e) { 
     throw new InflateException("Error inflating font XML", e); 
    } finally { 
     if (parser != null) 
      parser.close(); 
    } 
} 

public Typeface get(String family, int style) { 
    for (Font font: mFonts) { 
     for (String familyName : font.families) { 
      if (familyName.equals(family)) { 
       // if no style in specified, return normal style. 
       if (style == -1) 
        style = Typeface.NORMAL; 
       for (FontStyle fontStyle : font.styles) { 
        if (fontStyle.style == style) 
         return fontStyle.font; 
       } 
      } 
     } 
    } 
    return mDefaultFont; 
} 
अधिक कोड और नमूने के लिए

सिर्फ देखने के here

0
setContentView(R.layout.activity_main); 
    custfont(this, findViewById(android.R.id.content)); 



private void custfont(final Context context, View v) 
{ 
    try 
    { 
     if (v instanceof ViewGroup) 
     { 
      ViewGroup vg = (ViewGroup) v; 

      for (int i = 0; i < vg.getChildCount(); i++) 
      { 
       View child = vg.getChildAt(i); 
       overrideFonts(context, child); 
      } 
     } 
     else if (v instanceof TextView) 
     { 
      ((TextView) v).setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/ostrichblack.ttf")); 
     } 
    } 
    catch (Exception e) 
    { 
    } 
} 
0
import android.content.Context; 
import android.graphics.Typeface; 

/** 
* Created by Sanjeev Kumar on 4/18/2017. 
*/ 

public class FontManager { 
    public Context mContext; 

    public FontManager(Context context) { 
     this.mContext = context; 
    } 

    public Typeface getSquarkiFont() { 
     return Typeface.createFromAsset(mContext.getAssets(), "fonts/Squarki.ttf"); 
    } 

    public Typeface getLazySpringDayFont() { 
     return Typeface.createFromAsset(mContext.getAssets(), "fonts/LazySpringDay.ttf"); 
    } 
}