【问题标题】:what is the best practice to set android custom font?设置android自定义字体的最佳做法是什么?
【发布时间】:2017-08-06 12:46:00
【问题描述】:

我是android的初学者,我想改变一些控制字体。

第一种方式:

   TextView tx = (TextView)findViewById(R.id.textview1);

   Typeface custom_font = Typeface.createFromAsset(getAssets(),  "fonts/abc.ttf");

   tx.setTypeface(custom_font)

这种方式不好,因为你必须找到所有控件来设置字体

第二种方式: 创建从目标控件扩展的自定义控件

public class TextViewWithFont extends TextView {

        public TextViewWithFont(Context context, AttributeSet attrs) {
            super(context, attrs);
            Typeface custom_font = Typeface.createFromAsset(getAssets(),  "fonts/abc.ttf")
            this.setTypeface(custom_font);
        }

        public TextViewWithFont(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            Typeface custom_font = Typeface.createFromAsset(getAssets(),  "fonts/abc.ttf")
            this.setTypeface(custom_font);
        }

        public TextViewWithFont(Context context) {
            super(context);
            Typeface custom_font = Typeface.createFromAsset(getAssets(),  "fonts/abc.ttf")
            this.setTypeface(custom_font);
        }

第三种方式:使用书法库

所以,伙计们,你有什么想法?哪个更好,你知道改变字体的其他方法吗?

【问题讨论】:

  • 我通过在 Application 类中创建和初始化一个静态 TypeFace 来做到这一点。然后在任何地方使用它作为 tx.setTypeface(custom_font)
  • 你想在布局 XML 中也设置这个字体还是只在 Java 中设置这个字体?
  • @MuthukrishnanRajendran 感谢您的回复.. 我想在布局 xml 中使用
  • @NishinRaj 感谢您的回复。你能举个例子说明你是做什么的吗?
  • @hosseinderakhshan 无法将代码添加为注释,因为它不清楚。所以我将其添加为答案。

标签: android fonts custom-font


【解决方案1】:

这里我只是给出一些提示,如何在 XML 布局中使用自定义字体。

在本例中,我使用的是 Roboto 字体,您可以根据自己的要求进行更改。

首先,像这样在 attr 中创建自定义字体样式,

   <declare-styleable name="TCTextView">
        <attr name="fontType">
            <enum name="RobotoBold" value="0" />
            <enum name="RobotoBoldItalic" value="1" />
        </attr>
    </declare-styleable>

像这样创建您的自定义 TextView,

public class MKTextView extends TextView {

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

        init(context, attrs);
    }

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

    public MKTextView(Context context, AttributeSet attrs) {
        super(context, attrs, 0);

        init(context, attrs);
    }

    /**
     * To initialize the default value to the component.
     *
     * @param context
     * @param attrs
     */
    private void init(Context context, AttributeSet attrs) {
        Typeface typeface = FontUtil.getTCFont(context, attrs);

        if (typeface != null) {
            setTypeface(typeface);
        }

    }
}

使用这个 Helper Util 类来获取和设置自定义字体

public class FontUtil {

    /**
     * To set the custom font based on the attribute settings.
     *
     * @param ctx
     *         Context
     * @param attrs
     *         attribute setting for the components.
     *
     * @return {@link Typeface}
     */
    public static Typeface getTCFont(Context ctx, AttributeSet attrs) {
        TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.MKTextView);

        int value = a.getInt(R.styleable.MKTextView_fontType, 0);

        final String customFont = getFontName(value);

        a.recycle();

        return getTCFont(ctx, customFont);
    }

    /**
     * Get the Font name in assert folder based in {@link Fonts}
     *
     * @param fonts
     *         {@link Fonts}
     *
     * @return Font name in assert folder
     */
    private static String getFontName(int fonts) {
        final String customFont;
        switch (fonts) {
            case 0:
                customFont = "Roboto-Bold.ttf";
                break;
            case 1:
                customFont = "Roboto-BoldItalic.ttf";
                break;
            default:
                customFont = "Roboto-BoldItalic.ttf";
                break;
        }

        return customFont;
    }

    /**
     * To get the {@link Typeface} based on the font type(in asserts/fonts).
     *
     * @param ctx
     *         Context
     * @param asset
     *         font name in fonts folder
     *
     * @return {@link Typeface}
     */
    private static Typeface getTCFont(Context ctx, String asset) {
        Typeface tf = null;

        try {
            tf = Typeface.createFromAsset(ctx.getAssets(), "fonts/" + asset);
        } catch (Exception e) {
            return null;
        }

        return tf;
    }
}

你可以像这样在你的 XML 布局中使用,

<in.muthu.stackoverflow.font.MKTextView
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Text"
    app:fontType="RobotoBold"/>

【讨论】:

  • 非常感谢您的回复.. 这样我必须实现我的所有控件,如 MKTextView.. 你认为这是最好的方法吗?你对这个方法有问题吗?
  • 这只是一个自定义视图,我在所有项目中都在使用,我从未遇到任何问题。
【解决方案2】:

这是应用类

public class AppController extends Application {
    public static Typeface customTF;
    @Override
    public void onCreate() {
        super.onCreate();
        customTF = Typeface.createFromAsset(getAssets(), "fonts/custom_font.ttf");

    }

}

将类名添加到清单文件中的应用程序标签

<application
        android:name=".AppController"
        ...
        ...
        >

使用字体作为

tvTitle.setTypeface(AppController.customTF);

【讨论】:

  • 非常感谢.. 但是在这种方法中,如果我理解了您的观点,我们必须在我的表单中找到我的所有控件,然后为所有控件设置 setTypeface.. 是真的吗?
猜你喜欢
  • 2013-01-08
  • 2014-06-16
  • 2013-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-11
  • 2015-12-11
相关资源
最近更新 更多