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

19

我想改变按钮文本上显示的字体,我已经在屏幕上的文本,文本视图中完成了这个操作,但是找不到任何关于将其应用于按钮的信息或帮助。

我是一个新手,所以提供代码会非常感激。这是我用于文本视图的内容,但我如何更改按钮字体?

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

感谢 Lucy x

6个回答

52

按键 是一个文本视图,因此处理方式与文本视图相同:

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

6
我用这种方式来创建按钮,可以正常工作(同样适用于TextView)...
 Button enter=(Button) findViewById(R.id.enter);
 Typeface type=Typeface.createFromAsset(getAssets(), "arial.ttf");
 enter.setTypeface(type);

希望这能帮到您...


5
如果您计划将相同的字体添加到多个按钮中,我建议您全程实现它作为样式并子类化按钮:
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;
    }
}

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>

这可能看起来是很多工作,但是当你有几十个按钮和文本框需要改变字体时,你会感谢我的。


这是非常繁重的工作,你可以轻松避免它。 - NSV.

1
通过创建如下所示的自定义按钮:
  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);
    }
}

强烈不可用 - NSV.
抱歉,好主意。 - NSV.

1
另一个以上解决方案的替代方案:

(保留HTML标签)
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);

0

步骤: 1 - 创建新的Android资源目录。 2 - 选择字体类型(注意,在类型选择器中尝试输入“font”即可找到。) 3 - 将名称保留为“font”。 4 - 创建。

5 - 取出您的TTF文件(可以从互联网上下载) 6- 将其粘贴到Android的字体文件夹中 7 - 转到您想要使用字体的任何位置,并输入:

android:fontfamily="@font/yourfont.ttf"

8 - 如果您想在整个项目中使用它,这对于非默认字体项目非常方便,请转到themes.xml并在您的主题中编写此行:

<item name="android:fontFamily">@font/segoe_ui</item>

然而,如果您想以编程方式实现它,则@Konstatin Burov的答案是您最好的选择。

通过XML,我已经向您通报了。 注意:对于那些想知道如何工作的人, a)对于主题,这将为整个项目设置设置,并且ttf文件与android兼容。 b)对于特定用途,所有属性(不仅仅是按钮!而是任何需要显示文本的地方)都可以使用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接