如何使用Roboto字体在TextView中? [英] How to use Roboto Font in TextView?
本文介绍了如何使用Roboto字体在TextView中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用roboto字体类型,我的文字的看法? 我想从XML做到这一点,我的应用程序上面支持4.1。 下面是一些东西,我想:
<样式名称=BubbleNumber>
<项目名称=机器人:TEXTSTYLE>正常< /项目>
<项目名称=机器人:fontFamily中>无衬线< /项目>
<项目名称=机器人:TEXTSIZE> 14sp< /项目>
<项目名称=机器人:文字颜色> @色/ bubble_text_color< /项目>
< /风格>
解决方案
机器人已经是默认的字体类型(从安卓4.0开始) 看到 http://developer.android.com/design/style/typography.html
另外,你必须编程设置字体。
因此,我建议你写一个类:
公共类StyledTextView扩展TextView的{
公共StyledTextView(上下文的背景下){
超(上下文);
风格(背景);
}
公共StyledTextView(上下文的背景下,ATTRS的AttributeSet){
超(背景下,ATTRS);
风格(背景);
}
公共StyledTextView(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
超(背景下,ATTRS,defStyle);
风格(背景);
}
私人无效风格(上下文的背景下){
字样TF = Typeface.createFromAsset(context.getAssets()
字体/ roboto.ttf);
setTypeface(TF);
}
}
然后你可以简单地使用它在你的正常XML布局来代替正常的TextView
<的LinearLayout
机器人:宽=match_parent
机器人:身高=match_parent>
< com.your.packakge.StyledTextView
机器人:宽=match_parent
机器人:身高=match_parent/>
< / LinearLayout中>
how to use roboto font type for my text views ? I want to do it from xml and my app supports 4.1 above. below is something which I tried:
<style name="BubbleNumber">
<item name="android:textStyle">normal</item>
<item name="android:fontFamily">sans-serif</item>
<item name="android:textSize">14sp</item>
<item name="android:textColor">@color/bubble_text_color</item>
</style>
解决方案
Robot is already the default font type (starting from Android 4.0) see http://developer.android.com/design/style/typography.html
Otherwise you have to set the font programatically.
So I would recommend you to write a class:
public class StyledTextView extends TextView {
public StyledTextView(Context context) {
super(context);
style(context);
}
public StyledTextView(Context context, AttributeSet attrs) {
super(context, attrs);
style(context);
}
public StyledTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
style(context);
}
private void style(Context context) {
Typeface tf = Typeface.createFromAsset(context.getAssets(),
"fonts/roboto.ttf");
setTypeface(tf);
}
}
Then you can simply use it in your normal XML layout to replace the normal TextView
<LinearLayout
android:width="match_parent"
android:height="match_parent" >
<com.your.packakge.StyledTextView
android:width="match_parent"
android:height="match_parent" />
</LinearLayout>
这篇关于如何使用Roboto字体在TextView中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文