Android - Using Custom Font

后端 未结 21 1665
别跟我提以往
别跟我提以往 2020-11-22 04:38

I applied a custom font to a TextView, but it doesn\'t seems to change the typeface.

Here is my code:

    Typeface myTypeface = Typeface         


        
相关标签:
21条回答
  • 2020-11-22 05:14

    I know there are good answers already, but here's a fully working implementation.

    Here's the custom text view:

    package com.mycompany.myapp.widget;
    
    /**
     * Text view with a custom font.
     * <p/>
     * In the XML, use something like {@code customAttrs:customFont="roboto-thin"}. The list of fonts
     * that are currently supported are defined in the enum {@link CustomFont}. Remember to also add
     * {@code xmlns:customAttrs="http://schemas.android.com/apk/res-auto"} in the header.
     */
    public class CustomFontTextView extends TextView {
    
        private static final String sScheme =
                "http://schemas.android.com/apk/res-auto";
        private static final String sAttribute = "customFont";
    
        static enum CustomFont {
            ROBOTO_THIN("fonts/Roboto-Thin.ttf"),
            ROBOTO_LIGHT("fonts/Roboto-Light.ttf");
    
            private final String fileName;
    
            CustomFont(String fileName) {
                this.fileName = fileName;
            }
    
            static CustomFont fromString(String fontName) {
                return CustomFont.valueOf(fontName.toUpperCase(Locale.US));
            }
    
            public Typeface asTypeface(Context context) {
                return Typeface.createFromAsset(context.getAssets(), fileName);
            }
        }
    
        public CustomFontTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
    
            if (isInEditMode()) {
                return;
            } else {
                final String fontName = attrs.getAttributeValue(sScheme, sAttribute);
    
                if (fontName == null) {
                    throw new IllegalArgumentException("You must provide \"" + sAttribute + "\" for your text view");
                } else {
                    final Typeface customTypeface = CustomFont.fromString(fontName).asTypeface(context);
                    setTypeface(customTypeface);
                }
            }
        }
    }
    

    Here's the custom attributes. This should go to your res/attrs.xml file:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <declare-styleable name="CustomFontTextView">
            <attr name="customFont" format="string"/>
        </declare-styleable>
    </resources>
    

    And here's how you use it. I'll use a relative layout to wrap it and show the customAttr declaration, but it could obviously be whatever layout you already have.

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:customAttrs="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <com.mycompany.myapp.widget.CustomFontTextView
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="foobar"
             customAttrs:customFont="roboto_thin" />
    
    </RelativeLayout>
    
    0 讨论(0)
  • 2020-11-22 05:17

    On Mobiletuts+ there is very good tutorial on Text formatting for Android. Quick Tip: Customize Android Fonts

    EDIT: Tested it myself now. Here is the solution. You can use a subfolder called fonts but it must go in the assets folder not the res folder. So

    assets/fonts

    Also make sure that the font ending I mean the ending of the font file itself is all lower case. In other words it should not be myFont.TTF but myfont.ttf this way must be in lower case

    0 讨论(0)
  • 2020-11-22 05:17

    You can use easy & simple EasyFonts third party library to set variety of custom fonts to your TextView. By using this library you should not have to worry about downloading and adding fonts into the assets/fonts folder. Also about Typeface object creation.

    Instead of

    Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/myFont.ttf");
    TextView myTextView = (TextView)findViewById(R.id.myTextView);
    myTextView.setTypeface(myTypeface);
    

    Simply:

    TextView myTextView = (TextView)findViewById(R.id.myTextView);
    myTextView.setTypeface(EasyFonts.robotoThin(this));
    

    This library also provides following font face.

    • Roboto
    • Droid Serif
    • Droid Robot
    • Freedom
    • Fun Raiser
    • Android Nation
    • Green Avocado
    • Recognition
    0 讨论(0)
  • I had the same problem, the TTF did not show up. I changed the font file, and with the same code it's working.

    0 讨论(0)
  • 2020-11-22 05:20

    You can use PixlUI at https://github.com/neopixl/PixlUI

    import their .jar and use it in XML

     <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="GearedSlab.ttf" />
    
    0 讨论(0)
  • 2020-11-22 05:21

    The best way to do it From Android O preview release is this way:

    It works only if you have android studio-2.4 or above

    1. Right-click the res folder and go to New > Android resource directory. The New
      Resource Directory window appears.
    2. In the Resource type list, select font, and then click OK.
    3. Add your font files in the font folder.The folder structure below generates R.font.dancing_script, R.font.la_la, and R.font.ba_ba.
    4. Double-click a font file to preview the file's fonts in the editor.

    Next we must create a font family:

    1. Right-click the font folder and go to New > Font resource file. The New Resource File window appears.
    2. Enter the File Name, and then click OK. The new font resource XML opens in the editor.
    3. Enclose each font file, style, and weight attribute in the font tag element. The following XML illustrates adding font-related attributes in the font resource XML:

    Adding fonts to a TextView:

       <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/hey_fontfamily"/>
    

    As from the documentation

    Working With Fonts

    All the steps are correct.

    0 讨论(0)
提交回复
热议问题