我有一个button
使用Android小部件创建。我想将按钮文本的字体设置为Helv Neue 67 Med Cond
。如何获取此字体并将其设置为android布局文件中的按钮文本?在Android中设置按钮文本字体
13
A
回答
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
字体并将其存储在资产文件夹中。让下载的字体是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 Android和Android - Using Custom Font。
0
这是一个很好的文章,我用了几次,工作原理: http://mobile.tutsplus.com/tutorials/android/customize-android-fonts/
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"/>
这可能看起来像一个可怕的很多工作,但你一旦你有几把你想要改变字体的按钮和文本框,就会感谢我。
相关问题
- 1. 设置按钮的文字。 Android开发
- 2. Android按钮未设置文字颜色
- 3. android在按钮中设置2个不同的字体大小
- 4. 将按钮的输出文本设置为特定的字体
- 5. 如何在按钮中设置文本中的图像 - android?
- 6. 如何在线程中设置Android中按钮上的文本?
- 7. 在Android中设置字体
- 8. 如何在Android中为按钮设置自定义文本?
- 9. 我如何设置json响应字符串android按钮文本
- 10. jWYSIWYG设置字体颜色按钮
- 11. 如何在Android中为特定文本视图设置字体?
- 12. extjs - 在较小的按钮上正确放置按钮内的字体文本
- 13. 如何在Android中设置按钮?
- 14. 设置在Android上按钮的文本使用框架布局
- 15. 在按钮对象内设置动态文本字段的值
- 16. 在Cakephp中设置按钮与按钮
- 17. 设置按钮内的文本格式
- 18. 设置按钮内的文本大小
- 19. 通过javascript设置按钮文本
- 20. 设置按钮文本不起作用
- 21. 如何设置文本长度按钮
- 22. JavaFX通过CSS设置按钮文本
- 23. 将按钮文本设置为问号
- 24. 设置字体的Android本地
- 25. 在Android中设置固定字体
- 26. 如何设置图像和文本在android中的按钮的中心
- 27. 如何为对话框按钮的文本设置字体大小
- 28. iPhone设置按钮文本和文本的位置
- 29. Android:如何设置按钮点击编辑文本的内容?
- 30. Android:使用edittext输入来设置按钮的文本?
是的。但我想要Helv Neue 67 Med Cond字体。我怎样才能得到这种字体? – Ajn 2011-06-16 13:20:01
不适用于自定义字体;扩展每个元素或查看使用自定义布局inflater。 – clwhisk 2015-04-22 16:27:09