Accessing a font under assets folder from XML file in Android

后端 未结 14 1167
暖寄归人
暖寄归人 2020-12-05 17:07

I am trying to do a application-wide font change and creating a style file to do so. In this file (below) I just want to change typeface value of TextAppearance style of And

相关标签:
14条回答
  • 2020-12-05 18:00

    You can access your font file from assets folder to xml file.

    android:fontFamily="fonts/roboto_regular.ttf"

    fonts is the sub folder in assets folder.

    0 讨论(0)
  • 2020-12-05 18:02

    hope use full to you:-

    TextView text = (TextView) findViewById(R.id.custom_font);
    Typeface font = Typeface.createFromAsset(getAssets(), "yourfont.ttf");
    text.setTypeface(font);
    
    0 讨论(0)
  • 2020-12-05 18:05

    1.Fisrt we can add assets folder> in that your font styles.ttfs in your app then
    2.write access code for fonts in strings like :

    <string name="fontregular">OpenSans-Light.ttf</string>
    <string name="fontmedium">OpenSans-Regular.ttf</string>
    

    3.Accessing some layout xml file textview code like this below:

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="@string/fontregular"
        android:textColor="@color/normalfont"
        android:textSize="@dimen/textregular"/>
    

    4.Add dimensions for font size :

    <dimen name="textregular">14sp</dimen>
    <dimen name="textheader">16sp</dimen>
    <dimen name="smalltext">12sp</dimen>
    <dimen name="littletext">10sp</dimen>
    <dimen name="hightfont">18sp</dimen>
    

    5.Add font color in colors :

    <color name="normalfont">#666</color>
    <color name="headerfont">#333</color>
    <color name="aquablue">#4da8e3</color>
    <color name="orange">#e96125</color>
    

    6.Then apply changes it is easy process to change hole app. Happy coding keep smile..

    0 讨论(0)
  • 2020-12-05 18:06

    //accessing font file in code,

    Typeface type = Typeface.createFromAsset(getResources().getAssets(),"fonts/arial.ttf");
    textView.setTypeface(type);//assign typeface to textview
    

    //in assets folder->fonts(foldername)->arial.ttf(font file name)

    0 讨论(0)
  • 2020-12-05 18:07

    I took droid kid's answer and made it work with ANY font, just by typing the font filename directly into the XML:

    layout.xml

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:custom="http://schemas.android.com/apk/res-auto" >
    
        <!-- where font file is at "assets/fonts/arial.ttf" -->
        <com.odbol.widgets.TextViewWithFont
            ...
            custom:fontFilePath="fonts/arial.ttf" />
    
    </RelativeLayout>
    

    Fonts.java

    public class Fonts {
    
        // use Weak so fonts are freed from memory when you stop using them
        private final static Map<String, Typeface> fonts = new WeakHashMap<>(5);
    
        /***
         * Returns a font at the given path within the assets directory.
         *
         * Caches fonts to save resources.
         *
         * @param context
         * @param fontPath Path to a font file relative to the assets directory, e.g. "fonts/Arial.ttf"
         * @return
         */
        public synchronized static Typeface getFont(Context context, String fontPath) {
            Typeface font = fonts.get(fontPath);
    
            if (font == null) {
                font = Typeface.createFromAsset(context.getAssets(), fontPath);
                fonts.put(fontPath, font);
            }
    
            return font;
        }
    }
    

    values/attrs.xml

    <resources>
    
        <declare-styleable name="TextViewWithFont">
            <!-- a path to a font, relative to the assets directory -->
            <attr name="fontFilePath" format="string" />
    
            <attr name="type">
                <enum name="bold" value="1"/>
                <enum name="italic" value="2"/>
            </attr>
        </declare-styleable>
    </resources>
    

    TextViewWithFont.java

    public class TextViewWithFont extends TextView {
        private int defaultDimension = 0;
        private int TYPE_BOLD = 1;
        private int TYPE_ITALIC = 2;
        private int fontType;
        private int fontName;
    
        public TextViewWithFont(Context context) {
            super(context);
            init(null, 0);
        }
        public TextViewWithFont(Context context, AttributeSet attrs) {
            super(context, attrs);
            init(attrs, 0);
        }
        public TextViewWithFont(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            init(attrs, defStyle);
        }
        private void init(AttributeSet attrs, int defStyle) {
            // Load attributes
            final TypedArray a = getContext().obtainStyledAttributes(
                    attrs, R.styleable.TextViewWithFont, defStyle, 0);
            String fontPath = a.getString(R.styleable.TextViewWithFont_fontFilePath);
            fontType = a.getInt(R.styleable.TextViewWithFont_type, defaultDimension);
            a.recycle();
    
            if (fontPath != null) {
                setFontType(Fonts.getFont(getContext(), fontPath));
            }
        }
        private void setFontType(Typeface font) {
            if (fontType == TYPE_BOLD) {
                setTypeface(font, Typeface.BOLD);
            } else if (fontType == TYPE_ITALIC) {
                setTypeface(font, Typeface.ITALIC);
            } else {
                setTypeface(font);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-05 18:11

    fount change is very basic functionality in android which is mostly needed to each and every application.so every one want to change only once in application that reduce our development time so i would suggest you to see this link FountChanger.class.

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