Android上的自定义字体和自定义Textview

Ali*_*lin 18 android custom-controls textview typeface

从我需要开发的应用程序,我收到了一个特定的字体,有很多文件,如FontName-Regular,FontName-Bold,FontName-it.我需要在应用程序的所有textview中使用它.首先,我认为这是一项简单的任务.看看SO,找到一个非常好的线程:这里

所以首先我喜欢:

public static void overrideFonts(final Context context, final View v) {
    try {
        if (v instanceof ViewGroup) {
            ViewGroup vg = (ViewGroup) v;
            for (int i = 0; i < vg.getChildCount(); i++) {
                View child = vg.getChildAt(i);
                overrideFonts(context, child);
            }
        } else if (v instanceof TextView) {
            ((TextView)v).setTypeface(FONT_REGULAR);
        }
    } catch (Exception e) {
        e.printStackTrace();
        // ignore
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的活动中onCreate期间调用此方法.我的应用程序中的每个textView都显示出字体和男孩,我很高兴能够轻松逃离.直到我进入一个屏幕,其中一些textviews需要Bold as Style(android:textStyle="bold").然后我意识到这个解决方案不能让我从资产中加载Font-Bold .ttf.

比较深入,看到一个很好的自定义TextView实现,在同样的SO问题:

public class MyTextView extends TextView {

    public MyTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyTextView(Context context) {
        super(context);
        init();
    }

    public void init() {

        Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font/chiller.ttf");
        setTypeface(tf ,1);

    }
    }
Run Code Online (Sandbox Code Playgroud)

这看起来更好.我的问题是:我如何检测 init()我的控件是否将Style设置为Bold,以便我可以分配所请求的TypeFace?

感谢您的时间.

LE.按照下面的示例,我将我的课程更新为:

public class MyTextView extends TextView {

    Typeface normalTypeface = Typeface.createFromAsset(getContext().getAssets(), Constants.FONT_REGULAR);
    Typeface boldTypeface = Typeface.createFromAsset(getContext().getAssets(),  Constants.FONT_BOLD);

    public MyTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyTextView(Context context) {
        super(context);
    }

    public void setTypeface(Typeface tf, int style) {
        if (style == Typeface.BOLD) {
            super.setTypeface(boldTypeface/*, -1*/);
        } else {
            super.setTypeface(normalTypeface/*, -1*/);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

好吧如果我调试,应用程序进入setTypeFace,它似乎应用粗体,但在我的布局上我看不到任何变化,而不是粗体.无论我使用什么字体,我的TextView都没有进行任何更改,并使用默认的android字体显示.我想知道为什么 ?

我已经在博客中总结的一切在这里在我的博客也许这将帮助别人.

Fra*_*amo 26

使用从XML属性检索的参数的TextView调用构造函数.因此,如果您想拦截此调用以强制使用自己的字体,则可以覆盖此方法,如下所示:setTypeface(Typeface tf, int style)styleandroid:textStyle

public void setTypeface(Typeface tf, int style) {
    Typeface normalTypeface = Typeface.createFromAsset(getContext().getAssets(), "font/your_normal_font.ttf");
    Typeface boldTypeface = Typeface.createFromAsset(getContext().getAssets(), "font/your_bold_font.ttf");

    if (style == Typeface.BOLD) {
        super.setTypeface(boldTypeface/*, -1*/);
    } else {
        super.setTypeface(normalTypeface/*, -1*/);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • @Alin:问题是在创建Typeface之前,构造函数调用`setTypeface()`!因此,在`setTypeface()`中移动`Typeface`s的创建,现在它将起作用! (2认同)

Moh*_*hin 9

您可以使用my CustomTextView来允许您在assets文件夹中指定字体文件名:

https://github.com/mafshin/CustomTextView

用法非常简单:

<com.my.app.CustomTextView
        xmlns:custom="http://schemas.android.com/apk/res/com.my.app"            
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Test text"
        android:id="@+id/testcustomview" 

        custom:fontAssetName="Politica XT.otf"
        />
Run Code Online (Sandbox Code Playgroud)