Accessing a font under assets folder from XML file

2019-01-14 10:50发布

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

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="NightRiderFont" parent="@android:style/TextAppearance">
        <item name="android:typeface"> /***help need here***/ </item>
    </style>
</resources>

However font is in "assets/fonts/". How can I access this font, so I can use that style as a theme to get rid of changing all TextViews by hand programatically.

As summary: How can I access 'a file from assets folder' in XML?

13条回答
Lonely孤独者°
2楼-- · 2019-01-14 11:24

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.

查看更多
一夜七次
3楼-- · 2019-01-14 11:25

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.

查看更多
小情绪 Triste *
4楼-- · 2019-01-14 11:29

Uses this function if you are using single font.

public static void applyFont(final Context context, final View root, final String fontName) {
        try {
            if (root instanceof ViewGroup) {
                ViewGroup viewGroup = (ViewGroup) root;
                for (int i = 0; i < viewGroup.getChildCount(); i++)
                    applyFont(context, viewGroup.getChildAt(i), fontName);
            } else if (root instanceof TextView)
                ((TextView) root).setTypeface(Typeface.createFromAsset(context.getAssets(), fontName));
        } catch (Exception e) {
            Log.e("ProjectName", String.format("Error occured when trying to apply %s font for %s view", fontName, root));
            e.printStackTrace();
        }
    }
查看更多
做个烂人
5楼-- · 2019-01-14 11:32

hope use full to you:-

TextView text = (TextView) findViewById(R.id.custom_font);
Typeface font = Typeface.createFromAsset(getAssets(), "yourfont.ttf");
text.setTypeface(font);
查看更多
啃猪蹄的小仙女
6楼-- · 2019-01-14 11:34

You Have just make Public calls And attached Method like this

public class TextViewFontType {

public Typeface typeface;

public void fontTextView(final TextView textView, final String fonttype) {
    typeface = Typeface.createFromAsset(textView.getContext().getAssets(), fonttype);
    textView.setTypeface(typeface);
}

have you use any where method TextView set font-family.

public class FontList {

    public static final String gothicbNormal="fonts/gothicb.ttf";
    public static final String gothicbBold="fonts/gothicb.ttf";
}

made FontList calss after you have just call methods any where with pass two parameter.

查看更多
霸刀☆藐视天下
7楼-- · 2019-01-14 11:37

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

查看更多
登录 后发表回答