set custom font through xml

后端 未结 5 1743
忘掉有多难
忘掉有多难 2021-01-07 09:17

how can i set a font, whose ttf resides in my assets folder through xml? I know how to do that programmatically but how can you do that via xml? Thanks in adva

相关标签:
5条回答
  • 2021-01-07 09:59

    You cannot do it using XML directly, however you can extend TextView and set a default font.

    package com.nannu;
    
    import android.content.Context;
    import android.graphics.Typeface;
    import android.util.AttributeSet;
    import android.widget.TextView;
    
    public class NanTV extends TextView{
    
        private Context c;
        public NanTV(Context c) {
            super(c);
            this.c = c;
            Typeface tfs = Typeface.createFromAsset(c.getAssets(),
                    "font/yourfont.ttf");
            setTypeface(tfs);
    
        }
        public NanTV(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            this.c = context;
            Typeface tfs = Typeface.createFromAsset(c.getAssets(),
                    "font/yourfont.ttf");
            setTypeface(tfs);
            // TODO Auto-generated constructor stub
        }
    
        public NanTV(Context context, AttributeSet attrs) {
            super(context, attrs);
            this.c = context;
            Typeface tfs = Typeface.createFromAsset(c.getAssets(),
                    "font/yourfont.ttf");
            setTypeface(tfs);
    
        }
    
    
    }
    

    And in your layout use new TextView

    <com.nannu.NanTV
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" 
    
            />
    

    I am posting this from my prev answer https://stackoverflow.com/a/11239305/1166537

    0 讨论(0)
  • 2021-01-07 10:01

    If you create your own TextView derivation, you could add an XML-attribute that the class handles the programmatical way. That way, you specify a certain font and it'll be set runtime, but you do it via XML. :)

    Otherwise, there's no known way to achieve the requested behaviour.

    0 讨论(0)
  • 2021-01-07 10:01

    I have created a library to solve this problem here: http://responsiveandroid.com/2012/03/15/custom-fonts-in-android-widgets.html

    You extend a TextView and set the name of the typeface in the xml. There is a downloadable sample.

    0 讨论(0)
  • 2021-01-07 10:11

    You can do this by writing your custom TextView, otherwise you will have to set it programmatically. For more details see this link

    0 讨论(0)
  • 2021-01-07 10:17

    You can include custom fonts under assets/fonts and then using the code from https://github.com/browep/AndroidCustomFontWidgets/

    you can specify the font in your XML, e.g.

    <com.github.browep.customfonts.view.FontableTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="This is in a custom font"
            app:font="MyFont-Bold.otf" />
    

    The code supports FontableTextView and FontableButton, but it's quite easy to extend to support other widget types if you require.

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