在android中设置Button文本字体

时间:2011-06-16 13:12:37

标签: android android-layout android-button android-fonts

我使用android小部件创建了button。我想将按钮文本的字体设置为Helv Neue 67 Med Cond。如何获取此字体并将其设置为android布局文件中的按钮文本?

7 个答案:

答案 0 :(得分:24)

我猜你可能已经找到了答案,但如果没有(对于其他开发人员),你可以这样做:

1.您想将“Helv Neue 67 Med Cond.ttf”保存到assets文件夹中。 然后

对于TextView

  TextView txt = (TextView) findViewById(R.id.custom_font);
  Typeface typeface = Typeface.createFromAsset(getAssets(), "Helv Neue 67 Med Cond.ttf");
  txt.setTypeface(typeface);

按钮

  Button n=(Button) findViewById(R.id.button1);
  Typeface typeface = Typeface.createFromAsset(getAssets(), "Helv Neue 67 Med Cond.ttf");
  n.setText("show");
  n.setTypeface(typeface);

答案 1 :(得分:16)

首先你必须将ttf文件放在assets文件夹中然后你可以使用下面的代码在TextView中设置自定义字体,就像你可以为Button做的那样:

TextView txt = (TextView) findViewById(R.id.custom_font);
Typeface font = Typeface.createFromAsset(getAssets(), "Helv Neue 67 Med Cond.ttf");
txt.setTypeface(font);

答案 2 :(得分:3)

如果您计划将相同的字体添加到多个按钮,我建议您一直执行并实现子类按钮:

public class ButtonPlus extends Button {

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

    public ButtonPlus(Context context, AttributeSet attrs) {
        super(context, attrs);
        applyCustomFont(context);
    }

    public ButtonPlus(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        applyCustomFont(context);
    }

    private void applyCustomFont(Context context) {
            Typeface customFont = FontCache.getTypeface("fonts/candy.ttf", context);
            setTypeface(customFont);
        }
    }

这是减少旧设备上内存使用量的FontCache:

public class FontCache {

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

    public static Typeface getTypeface(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;
    }
}

最后在布局中使用示例:

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

这可能看起来像是一项非常多的工作,但是一旦你想要改变字体的几个按钮和文本字段,你会感谢我。

此外,您可以在tutorial中看到此GitHub和示例。

答案 3 :(得分:2)

Android附带3种字体(Sans, Serif, Monospace),可以使用android:typeface=”FONT_NAME”访问。

要使用自定义字体,您应该使用

之类的代码
TextView txt = (TextView) findViewById(R.id.custom_font);
  Typeface typeface = Typeface.createFromAsset(getAssets(), "Helv Neue 67 Med Cond.ttf");
  txt.setTypeface(typeface);

一些类似的问题是Custom Fonts in AndroidAndroid - Using Custom Font

答案 4 :(得分:1)

您可以使用:

android:typeface="yourfont"

答案 5 :(得分:0)

您必须下载Helv Neue 67 Med Cond字体并将其存储在assets文件夹中。让下载的字体为myfont.ttf

使用以下代码设置字体

Typeface tf = Typeface.createFromAsset(getAssets(), "myfont.ttf");
        TextView TextViewWelcome = (TextView)findViewById(R.id.textViewWelcome);
        TextViewWelcome.setTypeface(tf);

由于 迪帕克

答案 6 :(得分:0)

这是一篇很好的文章,我多次使用它并且有效: http://mobile.tutsplus.com/tutorials/android/customize-android-fonts/

相关问题