如何使用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>
上一篇:在Android的ImageView和进度执行图像下载进度、图像、Android、ImageView
下一篇:转换&QUOT; R.id.myID&QUOT;从字符串到int值R.id.myID?字符串、id、QUOT、int