在Android的访问下,资产的文件夹中的字体从XML文件 [英] Accessing a font under assets folder from XML file in Android

查看:174
本文介绍了在Android的访问下,资产的文件夹中的字体从XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个应用程序范围的字体改变和创造一个样式文件这样做。在这个文件中(下)我只是想改变的Andr​​oid TextAppearance风格的字体值。

 < XML版本=1.0编码=UTF-8&GT?;
<资源>
    <样式名称=NightRiderFont父=@安卓风格/ TextAppearance>
        <项目名称=机器人:字体> / ***帮助在这里需要*** /< /项目>
    < /风格>
< /资源>
 

但字体是在资产/字体/。我怎样才能访问该字体,这样我就可以使用该风格为主题,以摆脱不断变化TextViews手工编程的。

由于摘要:?我怎样才能访问从资源文件夹中的文件中的XML

解决方案

在我的研究中,有没有办法外部字体添加到XML文件中。只有3默认字体可在XML

但你可以在Java中使用这种code使用。

 字样TF = Typeface.createFromAsset(getAssets(),字体/ verdana.ttf);
textfield.setTypeface(TF,Typeface.BOLD);
 

更新:

现在我找到一种方法,通过创建扩展TextView的,并使用了XML文件中的自定义类来做到这一点。

 公共类TextViewWithFont扩展TextView的{
    私人诠释defaultDimension = 0;
    私人诠释TYPE_BOLD = 1;
    私人诠释TYPE_ITALIC = 2;
    私人诠释FONT_ARIAL = 1;
    私人诠释FONT_OPEN_SANS = 2;
    私人诠释fontType;
    私人诠释的fontName;

    公共TextViewWithFont(上下文的背景下){
        超(上下文);
        初始化(空,0);
    }
    公共TextViewWithFont(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
        初始化(ATTRS,0);
    }
    公共TextViewWithFont(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
        超(背景下,ATTRS,defStyle);
        的init(ATTRS,defStyle);
    }
    私人无效的init(AttributeSet中的ATTRS,INT defStyle){
        //装载属性
        最后TypedArray A =的getContext()。obtainStyledAttributes(
                的attrs,R.styleable.font,defStyle,0);
        的fontName = a.getInt(R.styleable.font_name,defaultDimension);
        fontType = a.getInt(R.styleable.font_type,defaultDimension);
        a.recycle();
        所有MyApplication应用=(所有MyApplication)的getContext()getApplicationContext()。
        如果(的fontName == FONT_ARIAL){
            setFontType(应用.getArialFont());
        }否则,如果(的fontName == FONT_OPEN_SANS){
            setFontType(应用.getOpenSans());
        }
    }
    私人无效setFontType(字体字库){
        如果(fontType == TYPE_BOLD){
            setTypeface(字体,Typeface.BOLD);
        }否则,如果(fontType == TYPE_ITALIC){
            setTypeface(字体,Typeface.ITALIC);
        } 其他 {
            setTypeface(字体);
        }
    }
}
 

在XML

 < com.example.customwidgets.TextViewWithFont
        字体:名称=宋体
        字体:TYPE =黑体
        机器人:layout_width =WRAP_CONTENT
        机器人:文字=世界,你好
        机器人:填充=5DP
        机器人:layout_height =WRAP_CONTENT/>
 

不要忘记添加架构在你的XML根

 的xmlns:字体=htt​​p://schemas.android.com/apk/res-auto
 

和创建文件中值的目录是保持我们自定义attribues

 <资源>
    <申报,设置样式名称=字体>
        < attr指示NAME =输入>
        <枚举名=黑体值=1/>
            <枚举名=斜体值=2/>
        < / ATTR>
        < attr指示NAME =名与GT;
            <枚举名=宋体值=1/>
            <枚举名=OpenSans值=2/>
        < / ATTR>
    < /申报,设置样式>
< /资源>
 

更新:

  

发现了一些性能问题时,这个自定义视图中使用   列表视图,这是因为字体对象被创建每次   观点是加载。解决办法我发现是初始化应用程序的字体   类,并参阅该字体对象通过

  MyApplication的应用=(所有MyApplication)的getContext()getApplicationContext()。
 

应用程序类看起来像这样

 公共类MyApplication的扩展应用{

    私人字样arialFont,openSans;

    公共无效的onCreate(){
        super.onCreate();

        arialFont = Typeface.createFromAsset(getAssets(),QRUtils.FONT_ARIAL);
        openSans = Typeface.createFromAsset(getAssets(),QRUtils.FONT_OPEN_SANS);
    }

    公共字样getArialFont(){
        返回arialFont;
    }

    公共字样getOpenSans(){
        返回openSans;
    }
}
 

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?

解决方案

In my research, there is no way to add external font to the xml file. Only the 3 default font is available in xml

But you can use in java using this code.

Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/verdana.ttf");  
textfield.setTypeface(tf,Typeface.BOLD);

Update:

Now I find a way to do this by creating a custom class extending the TextView and use that in the xml file.

public class TextViewWithFont extends TextView {
    private int defaultDimension = 0;
    private int TYPE_BOLD = 1;
    private int TYPE_ITALIC = 2;
    private int FONT_ARIAL = 1;
    private int FONT_OPEN_SANS = 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.font, defStyle, 0);
        fontName = a.getInt(R.styleable.font_name, defaultDimension);
        fontType = a.getInt(R.styleable.font_type, defaultDimension);
        a.recycle();
        MyApplication application = (MyApplication ) getContext().getApplicationContext();
        if (fontName == FONT_ARIAL) {
            setFontType(application .getArialFont());
        } else if (fontName == FONT_OPEN_SANS) {
            setFontType(application .getOpenSans());
        }
    }
    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);
        }
    }
}

and in xml

<com.example.customwidgets.TextViewWithFont
        font:name="Arial"
        font:type="bold"
        android:layout_width="wrap_content"
        android:text="Hello world "
        android:padding="5dp"
        android:layout_height="wrap_content"/>

dont forget to add the schema in root of your xml

xmlns:font="http://schemas.android.com/apk/res-auto"

And create a file inside Values directory which is holding our custom attribues

<resources>
    <declare-styleable name="font">
        <attr name="type">
        <enum name="bold" value="1"/>
            <enum name="italic" value="2"/>
        </attr>
        <attr name="name">
            <enum name="Arial" value="1"/>
            <enum name="OpenSans" value="2"/>
        </attr>
    </declare-styleable>
</resources>

Update:

Found some performance issue when this custom view is used in listview, that is because the font Object is creating every time the view is loaded. Solution I found is to initialize the font in Application Class and refer that font object by

MyApplication application = (MyApplication) getContext().getApplicationContext();

Application class will look like this

public class MyApplication extends Application {

    private Typeface arialFont, openSans;

    public void onCreate() {
        super.onCreate();

        arialFont = Typeface.createFromAsset(getAssets(), QRUtils.FONT_ARIAL);
        openSans = Typeface.createFromAsset(getAssets(), QRUtils.FONT_OPEN_SANS);
    }

    public Typeface getArialFont() {
        return arialFont;
    }

    public Typeface getOpenSans() {
        return openSans;
    }
}

这篇关于在Android的访问下,资产的文件夹中的字体从XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆