如何设置在Android的一个按钮上的文字一个特定的字体? [英] How to set a particular font for a button text in android?

查看:359
本文介绍了如何设置在Android的一个按钮上的文字一个特定的字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的按钮上的文字是在铜板哥特灯的字体,我还没有碰到过一个简单干净的$ C $下一个简单的功能,因为这。帮帮我!

I want my button text to be in the Copperplate Gothic Light font and I yet have not come across a simple clean code for a simple function as this. Help!

PS:由于Android自带的阿里尔和其自身的一些其他字体我们需要导入(道歉因为即时通讯新的这个缺乏一个更好的词),我们希望使用的字体。这是我已经能够收集到然而,这就是线索,我结束。

PS: Since android comes with ariel and a few other fonts on its own we need to import (apologies for the lack of a better word since im new to this) the font we wish to use. This is all I have been able to gather till yet and this is where the trail ends for me.

帮助需要! :]

推荐答案

如果您计划在同一字体添加到几个按钮,我建议你一路走下去,并实现它作为一种风格和子键:

If you plan to add the same font to several buttons I suggest that you go all the way and implement it as a style and subclass button:

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);
    }
}

这是一个辅助类在一个TextView设置字体(记住,按钮TextView中的一个子类)的基础上com.my.package:字体属性:

This is a helper class to set a font on a TextView (remember, Button is a subclass of TextView) based on the com.my.package:font attribute:

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);
        }
    }

}

和这里的FontCache来减少对旧设备的内存使用情况的:

And here's the FontCache to reduce memory usage on older devices:

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 /价值/ attrs.xml我们定义自定义设置样式属性。

In res/values/attrs.xml we define the custom styleable attribute

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

最后,在布局实例使用:

And finally an example use in a layout:

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

而在RES /价值/ style.xml

And in 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>

这似乎是一个可怕的大量的工作,但你会感谢我,一旦你有几个要更改字体,按钮和文本框一把的。

This may seem like an awful lot of work, but you'll thank me once you have couple of handfuls of buttons and textfields that you want to change font on.

这篇关于如何设置在Android的一个按钮上的文字一个特定的字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆