前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android上的自定义字体 - 通过XML进行动态字体选择

Android上的自定义字体 - 通过XML进行动态字体选择

作者头像
code_horse
发布2018-07-02 11:24:58
1.5K0
发布2018-07-02 11:24:58
举报
文章被收录于专栏:Android NoteAndroid Note
前言

这是我们系列自定义字体在Android上的第2篇文章。在之前,我们已经看到如何使用自定义字体并将不同的样式应用于TextView。这次,我们将向TextView XML显示一个高级解决方案,以便动态设置字体,而不需要任何代码! 如果您没有阅读以前的文章,您可以去看看。这可能有助于您的理解,因为它们都是基于彼此的。

不同的字体

一般来说,我们喜欢使用一些美丽的字体希望让我们的应用程序脱颖而出。虽然在这篇文章中解释了使用多种字体的简单方法,但请谨慎行事。任何具有太多字体的UI都会变得混乱!通过XML直接设置字体非常方便,而不需要额外的Java代码。

1、准备自定义XML属性

由于我们想通过XML设置字体,Android没有适当的TextView属性,我们必须添加一个我们调用字体的自定义属性。第一步是在/values/文件夹中添加一个attrs.xml。内容如下所示:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomFontTextView">
        <attr name="font" format="string"/>
    </declare-styleable>
</resources>

所做的这一切都是让系统知道我们有一个自定义属性,字体名称属于CustomFontTextView类。从现在开始,我们可以在代码和XML中访问该属性。接下来,让我们在/values/strings.xml中添加我们要用作String资源的字体名称:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="font_name_acm">acm</string>
    <string name="font_name_rb">rb</string>
    <string name="font_name_rbl">rbl</string>
    <string name="font_name_rbold">rbold</string>
    <string name="font_name_rboldl">rboldl</string>
    <string name="font_name_rltalic">rltalic</string>
    <string name="font_name_rlight">rlight</string>
    <string name="font_name_rll">rll</string>
    <string name="font_name_rm">rm</string>
    <string name="font_name_rml">rml</string>
    <string name="font_name_rr">rr</string>
    <string name="font_name_rt">rt</string>
    <string name="font_name_rtl">rtl</string>
    <string name="font_name_br">br</string>
    <string name="font_name_zz">zz</string>
</resources>

准备工作已经完成了,下面就要开始自定义了。

2、使用字体 - 属性

我们需要在xml文件中添加一行:

代码语言:javascript
复制
xmlns:app="http://schemas.android.com/apk/res-auto"  

在您的最高视图层次元素。例如,如果您的视图嵌套在LinearLayout中,则它将如下所示:

代码语言:javascript
复制
<LinearLayout  
    xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
...>

XML该行允许您通过使用app:font访问该字体属性,例如:

代码语言:javascript
复制
<com.custom.typeface.widget.CustomFontTextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_margin="12dp"
      android:text="www.baidu.com"
      android:textSize="18sp"
      app:font="@string/font_name_rlight"/>
3、实现CustomFontTextView

首先,我们必须获取一个包含用getsStyledAttributes()的所有属性的数组:

代码语言:javascript
复制
 TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomFontTextView);

最后一个参数是引用我们创建的attrs.xml文件作为第一步!现在我们只需要在attributeArray上使用getString()函数来获取指定的字体名称:

代码语言:javascript
复制
 String fontName = typedArray.getString(R.styleable.CustomFontTextView_font);

最后,我们必须在CustomFontTextView中扩展我们的逻辑,以正确设置字体。重要的逻辑发生在initCustomFontTextView()和selectTypeface()中。整个代码看起来像是这样的:

代码语言:javascript
复制
public class CustomFontTextView extends android.support.v7.widget.AppCompatTextView {
 
    public CustomFontTextView(Context context) {
        this(context, null);
    }

    public CustomFontTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomFontTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initCustomFontTextView(context, attrs);
    }

    private void initCustomFontTextView(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomFontTextView);
        String fontName = typedArray.getString(R.styleable.CustomFontTextView_font);
        int textStyle = attrs.getAttributeIntValue(ANDROID_SCHEMA, "textStyle", Typeface.NORMAL);
        Typeface typeface = selectTypeFace(context, fontName, textStyle);
        setTypeface(typeface);
        typedArray.recycle();
    }

    private Typeface selectTypeFace(Context context, String fontName, int textStyle) {
        if (fontName.contentEquals(context.getString(R.string.font_name_acm))) {
            return FontCache.getTypeface("acm.ttf", context);
        } else if (!TextUtils.isEmpty(fontName)) {
            switch (fontName) {
                case "zz":
                    return FontCache.getTypeface("zz.ttf", context);
                case "br":
                    return FontCache.getTypeface("bariol_regular.otf", context);
                case "rb":
                    return FontCache.getTypeface("rb.ttf", context);
                case "rbl":
                    return FontCache.getTypeface("rbl.ttf", context);
                case "rbold":
                    return FontCache.getTypeface("rbold.ttf", context);
                case "rboldl":
                    return FontCache.getTypeface("rboldl.ttf", context);
                case "rlight":
                    return FontCache.getTypeface("rlight.ttf", context);
                case "rll":
                    return FontCache.getTypeface("rll.ttf", context);
                case "rltalic":
                    return FontCache.getTypeface("rltalic.ttf", context);
                case "rm":
                    return FontCache.getTypeface("rm.ttf", context);
                case "rml":
                    return FontCache.getTypeface("rml.ttf", context);
                case "rr":
                    return FontCache.getTypeface("rr.ttf", context);
                case "rt":
                    return FontCache.getTypeface("rt.ttf", context);
                case "rtl":
                    return FontCache.getTypeface("rtl.ttf", context);
                default:
                    return FontCache.getTypeface("acm.ttf", context);
            }
        } else {
            return null;
        }
    }
}

这里的FontCache在上一篇文章中有的,不太明白的可以去看看

4、查看结果
代码语言:javascript
复制
<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">


            <TextView
                android:id="@+id/tv1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="12dp"
                android:text="中文自定义字体"
                android:textSize="18sp"/>

            <com.custom.typeface.widget.CustomFontTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="12dp"
                android:text="www.baidu.com"
                android:textSize="18sp"
                app:font="@string/font_name_zz"
                />


            <com.custom.typeface.widget.CustomFontTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="12dp"
                android:text="www.baidu.com"
                android:textSize="18sp"
                app:font="@string/font_name_rboldl"/>

            <com.custom.typeface.widget.CustomFontTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="12dp"
                android:text="www.baidu.com"
                android:textSize="18sp"
                app:font="@string/font_name_rltalic"/>

            <com.custom.typeface.widget.CustomFontTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="12dp"
                android:text="www.baidu.com"
                android:textSize="18sp"
                app:font="@string/font_name_rlight"/>


            <com.custom.typeface.widget.CustomFontTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="12dp"
                android:text="www.baidu.com"
                android:textSize="18sp"
                app:font="@string/font_name_rm"/>


            <com.custom.typeface.widget.CustomFontTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="12dp"
                android:text="www.baidu.com"
                android:textSize="18sp"
                app:font="@string/font_name_rt"/>

        </LinearLayout>

运行起来的效果看起来像是这样的:

动态自定义字体.png

自定义字体TextView到这里就结束了。

快乐工作,享受编程!
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.09.13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 不同的字体
  • 1、准备自定义XML属性
  • 2、使用字体 - 属性
  • 3、实现CustomFontTextView
  • 4、查看结果
    • 快乐工作,享受编程!
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档