android - 如何为android中的按钮文本设置特定字体?

标签 android button text fonts

我希望我的按钮文本采用 Copperplate Gothic Light 字体,但我还没有遇到这样一个简单功能的简单干净代码。帮助!

PS:由于 android 自带 ariel 和其他一些字体,我们需要 import (抱歉没有更好的词,因为我是新手)我们想要的字体使用。到目前为止,这就是我所能收集到的所有内容,这就是我的终点。

最佳答案

如果你打算为几个按钮添加相同的字体,我建议你一路实现,并将其实现为样式和子类按钮:

public class ButtonPlus extends Button {

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

    public ButtonPlus(Context context, AttributeSet attrs) {
        super(context, attrs);
        CustomFontHelper.setCustomFont(this, context, attrs);
    }

    public ButtonPlus(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        CustomFontHelper.setCustomFont(this, context, attrs);
    }
}

这是一个帮助类,用于根据 com.my.package:font 属性在 TextView 上设置字体(记住,Button 是 TextView 的子类):

public class CustomFontHelper {

    /**
     * Sets a font on a textview based on the custom com.my.package:font attribute
     * If the custom font attribute isn't found in the attributes nothing happens
     * @param textview
     * @param context
     * @param attrs
     */
    public static void setCustomFont(TextView textview, Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomFont);
        String font = a.getString(R.styleable.CustomFont_font);
        setCustomFont(textview, font, context);
        a.recycle();
    }

    /**
     * Sets a font on a textview
     * @param textview
     * @param font
     * @param context
     */
    public static void setCustomFont(TextView textview, String font, Context context) {
        if(font == null) {
            return;
        }
        Typeface tf = FontCache.get(font, context);
        if(tf != null) {
            textview.setTypeface(tf);
        }
    }

}

这里是 reduce memory usage on older devices 的 FontCache :

public class FontCache {

    private static Hashtable<String, Typeface> fontCache = new Hashtable<String, Typeface>();

    public static Typeface get(String name, Context context) {
        Typeface tf = fontCache.get(name);
        if(tf == null) {
            try {
                tf = Typeface.createFromAsset(context.getAssets(), name);
            }
            catch (Exception e) {
                return null;
            }
            fontCache.put(name, tf);
        }
        return tf;
    }
}

在 res/values/attrs.xml 我们定义了自定义的 styleable 属性

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomFont">
        <attr name="font" format="string"/>
    </declare-styleable>
</resources>

最后是布局中的使用示例:

    <com.my.package.buttons.ButtonPlus
        style="@style/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_sometext"/>

在 res/values/style.xml 中

<style name="button" parent="@android:style/Widget.Button">
    <item name="com.my.package:font">fonts/copperplate_gothic_light.TTF</item>
</style>

这似乎是一项非常艰巨的工作,但是一旦您拥有几个想要更改字体的按钮和文本字段,您就会感谢我。

关于android - 如何为android中的按钮文本设置特定字体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16648190/

相关文章:

android - 我可以通过 native java.net.Socket 连接 Android 客户端与使用 socket.io 的 Node js 服务器吗?

android - 在 gridview 数组上叠加单个图像

java - 使用按钮在jsp中调用java函数

image - 我看不到我的 wicket 项目的任何图像,我只看到图像名称?

java - Android - Firebase : code equivalent to list all users of databasereference as swift

android - 如何在TextView上将数字设置为图像?

linux - 仅使用 k1,1 执行排序

css - 我如何使 (.date) 文本背景颜色渐变(淡出)?

c# - 如何以编程方式将按钮添加到 gridview 并将其分配给特定的代码隐藏函数?

android - 如何让一切眼前一亮?