2011-06-16 82 views
13

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

回答

13

首先,你必须把资产的文件夹中的ttf文件,然后你可以用下面的代码来设置自定义字体中的TextView,相同的方式可以为按钮做:

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

您可以使用:

android:typeface="yourfont" 
+0

是的。但我想要Helv Neue 67 Med Cond字体。我怎样才能得到这种字体? – Ajn 2011-06-16 13:20:01

+0

不适用于自定义字体;扩展每个元素或查看使用自定义布局inflater。 – clwhisk 2015-04-22 16:27:09

0

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

使用下面的代码设置字体

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

感谢 迪帕克

2

的Android自带的3种字体(Sans, Serif, Monospace)可使用android:typeface=”FONT_NAME”被accesed。

对于使用自定义字体,你应该使用类似的代码

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

25

我猜你可能已经找到了答案,但如果没有(和其他开发者),你可以像下面这样做:

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

如果您打算添加相同的字体的几个按钮,我建议你去所有的方式和实施小类按钮:

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"/> 

这可能看起来像一个可怕的很多工作,但你一旦你有几把你想要改变字体的按钮和文本框,就会感谢我。

你也可以在GitHub看到这个tutorial和一个例子。