Android 帮助更改按钮字体类型,如何?
我想更改按钮文本上显示的字体,我已经设法使用屏幕上的文本、文本视图来做到这一点,但找不到任何信息,也无法帮助将其应用到按钮。
我是新手,所以提供这样做的代码,将不胜感激。这就是我用于文本视图的内容,但是如何更改按钮字体?
TextView txt = (TextView) findViewById(R.id.custom_font);
Typeface font = Typeface.createFromAsset(getAssets(), "1543Humane_jenson_bold.TTF");
txt.setTypeface(font);
谢谢 露西 x
i would like to change the font that appears on the button text, i have managed to do this with text on the screen, textview, but cannot find any info, or help with applying this to a button.
I 'am novice, so providing the code to do so, would be much appreciated. This is what i'am using for the textview, but how do i change the button font?
TextView txt = (TextView) findViewById(R.id.custom_font);
Typeface font = Typeface.createFromAsset(getAssets(), "1543Humane_jenson_bold.TTF");
txt.setTypeface(font);
Thanks
Lucy
x
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
按钮 IS-A TextView,因此只需像使用 TextView 一样操作即可:
Button IS-A TextView, so just do it as with a TextView:
我像这样使用按钮并且它有效(与 TextView 相同)..
希望它有帮助...
I use like this for button and it worked (same as for TextView) ..
hope it helps...
如果您打算向多个按钮添加相同的字体,我建议您一路将其实现为样式和子类按钮:
这是一个在 TextView 上设置字体的辅助类(请记住,Button 是 TextView 的子类) )基于 com.my.package:font 属性:
这里是 FontCache,用于减少旧设备上的内存使用:
在 res/values/attrs.xml 中,我们定义自定义 styleable 属性
最后是一个示例在布局中使用:
在 res/values/style.xml 中
这可能看起来工作量很大,但是一旦您有几个想要更改字体的按钮和文本字段,您就会感谢我。
If you plan to add the same font to several buttons I suggest that you go all the way and implement it as a style and subclass button:
This is a helper class to set a font on a TextView (remember, Button is a subclass of TextView) based on the com.my.package:font attribute:
And here's the FontCache to reduce memory usage on older devices:
In res/values/attrs.xml we define the custom styleable attribute
And finally an example use in a layout:
And in res/values/style.xml
This may seem like an awful lot of work, but you'll thank me once you have couple of handfuls of buttons and textfields that you want to change font on.
上述解决方案的另一种替代方案:
An other alternative for the above solutions:
通过创建自定义按钮,如下所示:
By creating a custom button as shown here:
步骤:
1 - 创建新的 Android 资源目录。
2 - 选择类型作为字体(注意,您尝试在类型选择器中输入字体,您会发现。)
3 - 保留名称作为字体。
4 - 创建。
5 - 获取您的TTF文件(可以从互联网下载)
6-将其粘贴到android的字体文件夹中
7 - 转到您想要使用字体的位置,然后输入:
8 - 如果您想在整个项目中执行此操作(这对于非默认字体项目非常方便),请转到主题.xml 并将此行写入您的主题:
但是,如果您想以编程方式执行此操作,那么 @Konstatin Burov 的回答是您最好的选择。
通过XML,我已经告诉你了。
注意:对于那些想知道这是如何工作的人,
a) 对于主题,这为您的整个项目设置设置,并且 ttf 文件与 android 兼容。
b)对于特定用途,所有属性(不仅仅是按钮!,而是任何需要显示文本的地方)都可以工作。
Steps:
1 - Create new Android Resource Directory.
2 - Select type as font(note, you try typing font in type selector u will find. )
3 - keep name as font.
4 - create.
5 - take your TTF file (can download from internet)
6- paste it in font folder of android
7 - go to where ever you want to use font, and put:
8 - if you wanna do it in whole project, which is very convinient for a non-default-font project, go to themes.xml and write this line in your theme:
However, if you want to do it programmatically then answer by @Konstatin Burov is your best bet.
By XML, I have already informed you .
Note: for those who want to know how this works,
a) for theme, this sets setting for your whole project, and ttf file IS compatible with android.
b) for specific usage, all attrs(not just buttons!, but any place U need to display text) it works.