如何使用没有Root的编程在Android设备中安装Font?

时间:2016-02-10 06:54:33

标签: android fonts typeface android-fonts android-typeface

我想在我的应用程序启动时在android设备中安装.ttf文件的自定义字体。实际上,当我运行我的应用程序时,我需要在我的Android设备中安装古吉拉特语字体。可能吗 ?如果是的话怎么样?谢谢。我不需要字体..我想在没有root设备的系统设置中安装

1 个答案:

答案 0 :(得分:0)

除非将shruti.ttf文件放入assert文件夹,否则没有其他方法可以在没有root的情况下从应用程序安装字体

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

您也可以通过扩展其他控件ex.TextView

来设置字体
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);
    }
}
}