2012-01-09 19 views
43

एंड्रॉइड में थीम्स में कस्टम फोंट जोड़ने का कोई तरीका है?एंड्रॉइड में थीम के लिए कस्टम फ़ॉन्ट जोड़ना

मैंने Quick Tip: Customize Android Fonts पढ़ा है, लेकिन यहां हमें प्रोग्राम में कस्टम फ़ॉन्ट जोड़ने के लिए प्रोग्राम करना है।

TextView txt = (TextView) findViewById(R.id.custom_font); 
Typeface font = Typeface.createFromAsset(getAssets(), "Chantelli_Antiqua.ttf"); 
txt.setTypeface(font); 

लेकिन मैं शैली/थीम द्वारा कस्टम फ़ॉन्ट सेट करना चाहता हूं।

उत्तर

-1

मुझे उम्मीद है कि यह आपका मतलब है, लेकिन यदि ऐसा नहीं है तो यह दूसरों के लिए एक अच्छा संदर्भ होना चाहिए।

** नोट: फ़ॉन्ट्स पाया जा सकता है कंप्यूटर/स्थानीय डिस्क (C:)/विंडोज/फ़ॉन्ट्स **

कॉपी फ़ॉन्ट कि आप ऊपर फ़ॉन्ट्स फ़ोल्डर में इस्तेमाल करते हैं और एक नव में पेस्ट करना चाहते हैं ग्रहण में संपत्ति फ़ोल्डर में फ़ोल्डर बनाया गया।

private void initTypeFace() 
    { 
     TypeFace tf = TypeFace.createFromAsset(getAsset(), 
         "Chantelli Antiqua.ttf"); 

     TextView txt = (TextView) findViewById(R.id.custom_font); 

     txt.setTypeface(tf); 
     example_button1.setTypeface(tf); 
     example_button2.setTypeface(tf); 
    } 
0

मुझे लगता है कि आपके सवाल का जवाब अतिरिक्त XML पैरामीटर, जो आप अपने विषयों को शामिल कर सकते हैं के साथ कस्टम TextView हो woulbe।

आप इसे प्रारंभ करने के लिए कन्स्ट्रक्टर टेक्स्ट व्यू (संदर्भ संदर्भ, विशेषता एसईटी एआरआरआर) में इस मान को पार्स कर सकते हैं। अपने विचारों के लिए कस्टम एटर्स को परिभाषित करने और उन्हें प्रारंभ करने के उदाहरण के लिए this लिंक देखें।

12

मुझे लगता है कि यह this question और this one का डुप्लिकेट है।

FontUtils.setCustomFont(findViewById(R.id.top_view), getAssets()); 

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

 <TextView 
      android:id="@+id/my_label" 
      android:tag="condensed" 
      android:text="@string/label" 
      ... /> 

तो सैद्धांतिक रूप से आप शैली बना सकते हैं और यह एक साथ उपयोग FontUtils/क्रम कोड के साथ कर सकते हैं

क्रम में मेरी गतिविधियों में, मैं कुछ इस तरह का उपयोग करें।

<style name="roboto_condensed"> 
    <item name="android:tag">condensed,your-own-css-like-language-here</item> 
</style> 

FontUtils वर्ग:

public class FontUtils { 
    private static Typeface normal; 

    private static Typeface bold; 

    private static Typeface condensed; 

    private static Typeface light; 

    private static void processsViewGroup(ViewGroup v, final int len) { 

    for (int i = 0; i < len; i++) { 
     final View c = v.getChildAt(i); 
     if (c instanceof TextView) { 
     setCustomFont((TextView) c); 
     } else if (c instanceof ViewGroup) { 
     setCustomFont((ViewGroup) c); 
     } 
    } 
    } 

    private static void setCustomFont(TextView c) { 
    Object tag = c.getTag(); 
    if (tag instanceof String) { 
     if (((String) tag).contains("bold")) { 
     c.setTypeface(bold); 
     return; 
     } 
     if (((String) tag).contains("condensed")) { 
     c.setTypeface(condensed); 
     return; 
     } 
     if (((String) tag).contains("light")) { 
     c.setTypeface(light); 
     return; 
     } 
    } 
    c.setTypeface(normal); 
    } 

    public static void setCustomFont(View topView, AssetManager assetsManager) { 
    if (normal == null || bold == null || condensed == null || light == null) { 
     normal = Typeface.createFromAsset(assetsManager, "fonts/roboto/Roboto-Regular.ttf"); 
     bold = Typeface.createFromAsset(assetsManager, "fonts/roboto/Roboto-Bold.ttf"); 
     condensed = Typeface.createFromAsset(assetsManager, "fonts/roboto/Roboto-Condensed.ttf"); 
     light = Typeface.createFromAsset(assetsManager, "fonts/roboto/Roboto-Light.ttf"); 
    } 

    if (topView instanceof ViewGroup) { 
     setCustomFont((ViewGroup) topView); 
    } else if (topView instanceof TextView) { 
     setCustomFont((TextView) topView); 
    } 
    } 

    private static void setCustomFont(ViewGroup v) { 
    final int len = v.getChildCount(); 
    processsViewGroup(v, len); 
    } 
} 
3

आप संपत्ति फ़ोल्डर में अपने कस्टम फ़ॉन्ट प्रकार शामिल हैं और वहां से नहीं निकाला जा सकता है।

Typeface helveticaBold; 
Typeface helveticaRegular; 
onCreate में

() निम्नलिखित कोड लिखने:

के रूप में घोषित टाइपफेस

editText.setTypeface(helveticaRegular); 
: अंत में

helveticaBold = Typeface.createFromAsset(getAssets(), "helvetica_bold.ttf"); 
helveticaRegular = Typeface.createFromAsset(getAssets(), "helvetica_regular.ttf"); 

, के रूप में TextView या EditText के पाठ के टाइपफेस सेट

यह है ...

+0

हाँ, लेकिन वह नहीं पूछ रहा है कि वह – user1010160

4

मेरे CustomTextView का उपयोग करके आप अपने XML लेआउट फ़ाइल में सीधे अपने assets फ़ोल्डर में एक फ़ॉन्ट फ़ाइल नाम निर्दिष्ट करते हैं।

मेरा जवाब here

19

दुर्भाग्य से, एंड्रॉयड त्वरित, आसान और साफ रास्ता अपने पूरे अनुप्रयोग के लिए फ़ॉन्ट बदलने के लिए आप देख रहे हैं प्रदान नहीं करता है। लेकिन हाल ही में मैंने इस मामले को देखा है और कुछ टूल्स बनाए हैं जो आपको बिना किसी कोडिंग के फ़ॉन्ट को बदलने की अनुमति देते हैं (आप इसे एक्सएमएल, शैलियों और यहां तक ​​कि टेक्स्ट उपस्थिति के माध्यम से भी कर सकते हैं)। वे समान समाधानों पर आधारित हैं जैसे कि आप यहां अन्य उत्तरों में देखते हैं, लेकिन अधिक लचीलापन की अनुमति देते हैं। आप this blog पर इसके बारे में सब कुछ पढ़ सकते हैं, और github प्रोजेक्ट here देखें।

यहां इन उपकरणों को लागू करने का एक उदाहरण दिया गया है। अपनी सभी फ़ॉन्ट फ़ाइलों को assets/fonts/ में रखें। फिर, उन फ़ॉन्ट्स को एक एक्सएमएल फ़ाइल (उदा। res/xml/fonts.xml) में घोषित करें और TypefaceManager.initialize(this, R.xml.fonts); (उदा।, आपके एप्लिकेशन क्लास के क्रिएट में) के साथ इस फ़ाइल को अपने ऐप में लोड करें। xml फ़ाइल इस तरह दिखता है:

<?xml version="1.0" encoding="utf-8"?> 
<familyset> 

    <!-- Some Font. Can be referenced with 'someFont' or 'aspergit' --> 
    <family> 
     <nameset> 
      <name>aspergit</name> 
      <name>someFont</name> 
     </nameset> 
     <fileset> 
      <file>Aspergit.ttf</file> 
      <file>Aspergit Bold.ttf</file> 
      <file>Aspergit Italic.ttf</file> 
      <file>Aspergit Bold Italic.ttf</file> 
     </fileset> 
    </family> 

    <!-- Another Font. Can be referenced with 'anotherFont' or 'bodoni' --> 
    <family> 
     <nameset> 
      <name>bodoni</name> 
      <name>anotherFont</name> 
     </nameset> 
     <fileset> 
      <file>BodoniFLF-Roman.ttf</file> 
      <file>BodoniFLF-Bold.ttf</file> 
     </fileset> 
    </family> 

</familyset> 

अब आप अपनी शैली या एक्सएमएल (आप उपकरण मैं उपर्युक्त का उपयोग प्रदान की गई) में इन फ़ॉन्ट उपयोग कर सकते हैं, अपने XML लेआउट में कस्टम TextView com.innovattic.font.FontTextView में flFont विशेषता सेट करके । नीचे आप देख सकते हैं कि आप एक फ़ॉन्ट सभी ग्रंथों के लिए अपने पूरे अनुप्रयोग में, बस संपादन res/values/styles.xml से आवेदन कर सकते हैं: साथ res/layout/layout.xml साथ

<?xml version="1.0" encoding="utf-8"?> 
<resources xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> 

    <!-- Application theme --> 
    <!-- Use a different parent if you don't want Holo Light --> 
    <style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar"> 
     <item name="android:textViewStyle">@style/MyTextViewStyle</item> 
    </style> 

    <!-- Style to use for ALL text views (including FontTextView) --> 
    <!-- Use a different parent if you don't want Holo Light --> 
    <style name="MyTextViewStyle" parent="@android:style/Widget.Holo.Light.TextView"> 
     <item name="android:textAppearance">@style/MyTextAppearance</item> 
    </style> 

    <!-- Text appearance to use for ALL text views (including FontTextView) --> 
    <!-- Use a different parent if you don't want Holo Light --> 
    <style name="MyTextAppearance" parent="@android:style/TextAppearance.Holo"> 
     <!-- Alternatively, reference this font with the name "aspergit" --> 
     <!-- Note that only our own TextView's will use the font attribute --> 
     <item name="flFont">someFont</item> 
     <item name="android:textStyle">bold|italic</item> 
    </style> 

    <!-- Alternative style, maybe for some other widget --> 
    <style name="StylishFont"> 
     <item name="flFont">anotherFont</item> 
     <item name="android:textStyle">normal</item> 
    </style> 

</resources> 

:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity" > 

    <!-- This text view is styled with the app theme --> 
    <com.innovattic.font.FontTextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="This uses my font in bold italic style" /> 

    <!-- This text view is styled here and overrides the app theme --> 
    <com.innovattic.font.FontTextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:flFont="anotherFont" 
     android:textStyle="normal" 
     android:text="This uses another font in normal style" /> 

    <!-- This text view is styled with a style and overrides the app theme --> 
    <com.innovattic.font.FontTextView 
     style="@style/StylishFont" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="This also uses another font in normal style" /> 

</LinearLayout> 

मत भूलना विषय लागू करने के लिए आपके एंड्रॉइड मेनिफेस्ट में।

+0

धन्यवाद! ग्रेट आलेख .. –

+0

जैसा कि आप जावा कोड का भी उपयोग कर रहे हैं, तो हम इसे अधिक सरल तरीके से कर सकते हैं –

+0

यह वास्तव में एंड्रॉइड में मूल होना चाहिए – Zach

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