Adding custom Font to Theme in Android

前端 未结 6 1847
故里飘歌
故里飘歌 2020-11-29 19:35

Is there any way to add custom fonts in Themes in Android?

I have read Quick Tip: Customize Android Fonts, but here we have to programmetrically add custom font to

相关标签:
6条回答
  • 2020-11-29 19:50

    I think the answer to your question woulbe be custom TextView with additional XML parameter, which you can include to your themes.

    You can parse this value in the constructor TextView(Context context, AttributeSet attrs) to initialize it. Check this link for example of defining custom attrs for your views and initializing them.

    0 讨论(0)
  • 2020-11-29 19:51

    Unfortunately, Android doesn't provide the quick, easy and clean way you're looking for to change the font for your entire app. But recently I've looked into this matter and created some tools that allow you to change the font without any coding (you can do it all through xml, styles and even text appearances). They're based on similar solutions like you see in the other answers here, but allow for far more flexibility. You can read all about it on this blog, and see the github project here.

    Here's an example of how to apply these tools. Put all your font files in assets/fonts/. Then, declare those fonts in an xml file (e.g. res/xml/fonts.xml) and load this file early in your app with TypefaceManager.initialize(this, R.xml.fonts); (e.g., in the onCreate of your Application class). The xml file looks like this:

    <?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>
    

    Now you can use these fonts in your style or xml (provided you use the tools I mentioned above), by setting the flFont attribute in the custom TextView com.innovattic.font.FontTextView in your xml layout. Below you can see how you can apply a font to all texts in your entire app, just by editing res/values/styles.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>
    

    With the accompanying res/layout/layout.xml:

    <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>
    

    Don't forget to apply the theme in your Android manifest.

    0 讨论(0)
  • I think this is duplicate of this question and this one.

    In my activities in runtime, I use something like this:

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

    In XML:

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

    So theoretically you can create style and use it together with FontUtils/runtime code.

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

    FontUtils class:

    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);
      }
    }
    
    0 讨论(0)
  • 2020-11-29 19:52

    By using my CustomTextView you specify a font file name in your assets folder directly in your XML layout file.

    My answer is here

    0 讨论(0)
  • 2020-11-29 19:59

    You can include your custom font type in the assets folder and retreive it from there.

    Declare the Typefaces as:

    Typeface helveticaBold;
    Typeface helveticaRegular;
    

    in onCreate() write the following code:

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

    lastly, set the typeface of the text of TextView or EditText as:

    editText.setTypeface(helveticaRegular);
    

    that's it...

    0 讨论(0)
  • 2020-11-29 20:08

    I hope this is what you meant, but if it is not it should be a good reference for others anyway.

    ** Note: Fonts can be found Computer/Local Disk (C:)/Windows/Fonts **

    Copy the font that you would like to use in the Fonts folder above and paste it into a newly created folder in the assets folder in the Eclipse.

        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 讨论(0)
提交回复
热议问题