दुर्भाग्य से, एंड्रॉयड त्वरित, आसान और साफ रास्ता अपने पूरे अनुप्रयोग के लिए फ़ॉन्ट बदलने के लिए आप देख रहे हैं प्रदान नहीं करता है। लेकिन हाल ही में मैंने इस मामले को देखा है और कुछ टूल्स बनाए हैं जो आपको बिना किसी कोडिंग के फ़ॉन्ट को बदलने की अनुमति देते हैं (आप इसे एक्सएमएल, शैलियों और यहां तक कि टेक्स्ट उपस्थिति के माध्यम से भी कर सकते हैं)। वे समान समाधानों पर आधारित हैं जैसे कि आप यहां अन्य उत्तरों में देखते हैं, लेकिन अधिक लचीलापन की अनुमति देते हैं। आप 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>
मत भूलना विषय लागू करने के लिए आपके एंड्रॉइड मेनिफेस्ट में।
स्रोत
2014-03-03 13:02:35
हाँ, लेकिन वह नहीं पूछ रहा है कि वह – user1010160