Android帮助更改按钮字体类型,如何?

Android帮助更改按钮字体类型,如何?,android,button,fonts,typeface,Android,Button,Fonts,Typeface,我想更改按钮文本上显示的字体,我已成功地使用屏幕上的文本、文本视图来执行此操作,但找不到任何信息,也找不到将此应用于按钮的帮助 我是新手,所以如果能提供这样做的代码,我将不胜感激。这是我在文本视图中使用的,但是如何更改按钮字体呢 TextView txt = (TextView) findViewById(R.id.custom_font); Typeface font = Typeface.createFromAsset(getAssets(), "1543Humane_jenson_bo

我想更改按钮文本上显示的字体,我已成功地使用屏幕上的文本、文本视图来执行此操作,但找不到任何信息,也找不到将此应用于按钮的帮助

我是新手,所以如果能提供这样做的代码,我将不胜感激。这是我在文本视图中使用的,但是如何更改按钮字体呢

TextView txt = (TextView) findViewById(R.id.custom_font);  
Typeface font = Typeface.createFromAsset(getAssets(), "1543Humane_jenson_bold.TTF");  
txt.setTypeface(font);
谢谢 露西
x

我像这样使用按钮,它工作正常(与TextView相同)

希望它有助于…

按钮文本视图,所以只需像使用文本视图一样:

Button txt = (Button) findViewById(R.id.custom_font);  
Typeface font = Typeface.createFromAsset(getAssets(), "1543Humane_jenson_bold.TTF");  
txt.setTypeface(font);

如果您计划将同一字体添加到多个按钮中,我建议您尽全力将其实现为样式和子类按钮:

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);
        }
    }
 }
下面是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;
    }
}
公共类FontCache{
私有静态哈希表fontCache=新哈希表();
公共静态字体get(字符串名称、上下文){
字体tf=fontCache.get(名称);
如果(tf==null){
试一试{
tf=Typeface.createFromAsset(context.getAssets(),name);
}
捕获(例外e){
返回null;
}
fontCache.put(名称,tf);
}
返回tf;
}
}
在res/values/attrs.xml中,我们定义了定制的可样式化属性

<?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>

字体/铜版\u哥特式\u light.TTF

这似乎是一项非常繁重的工作,但如果您有几把按钮和文本字段要更改字体,您将感谢我。

上述解决方案的另一种选择:

mYourButton = (Button) findViewById(R.id.Button_id);
mYourEditText = (EditText) findViewById(R.id.editText_id);

Typeface font = ResourcesCompat.getFont(getContext(), R.font.font_name.TTF);
mYourButton.setTypeface(font);
mYourEditText.setTypeface(font);

通过创建自定义按钮,如下所示:

  public class Button_Roboto_Regular extends Button {

    public Button_Roboto_Regular(Context context) {
        super(context);
        mTextFont(context);
    }

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

    public Button_Roboto_Regular(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mTextFont(context);
    }
    private void mTextFont(Context context) {
        Typeface face = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Regular_0.ttf");
        this.setTypeface(face);
    }
}
  public class Button_Roboto_Regular extends Button {

    public Button_Roboto_Regular(Context context) {
        super(context);
        mTextFont(context);
    }

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

    public Button_Roboto_Regular(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mTextFont(context);
    }
    private void mTextFont(Context context) {
        Typeface face = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Regular_0.ttf");
        this.setTypeface(face);
    }
}