Android字体设置 Roboto字体使用

一、自定义字体

1.android Typeface使用TTF字体文件设置字体

我们可以在程序中放入ttf字体文件,在程序中使用Typeface设置字体。
第一步,在assets目录下新建fonts目录,把ttf字体文件放到这。
第二步,程序中调用:

1. AssetManager mgr=getAssets();//得到AssetManager
2.  
3. Typeface tf=Typeface.createFromAsset(mgr, "fonts/ttf.ttf");//根据路径得到Typeface
4.  
5. tv=findViewById(R.id.textview);
6.  
7. tv.setTypeface(tf);//设置字体

 

2.在xml文件中使用android:textStyle=”bold” 可以将英文设置成粗体, 但是不能将中文设置成粗体,
将中文设置成粗体的方法是:

 

1. TextView tv = (TextView)findViewById(R.id.TextView01);
2. tv.getPaint().setFakeBoldText(true);//中文仿“粗体”--使用TextPaint的仿“粗体”设置setFakeBoldText为true。

 

注意:部分字体中文无效,虽然不会报错,但是对中文无效。

二、使用RoBoto

自从 Android 4.0后默认字体就使用了Roboto,下面介绍一下使用方法:

 

 

1. android:fontFamily="sans-serif" // roboto regular
2. android:fontFamily="sans-serif-light" // roboto light
3. android:fontFamily="sans-serif-condensed" // roboto condensed
4. android:fontFamily="sans-serif-thin" // roboto thin (android 4.2)
5. //in combination with
6. android:textStyle="normal|bold|italic"

 

可用的参数:

Regular
Italic
Bold
Bold-italic
Light
Light-italic
Thin
Thin-italic
Condensed regular
Condensed italic
Condensed bold
Condensed bold-italic

在开发中可能会涉及到对于组件的字体或文本的字体的修改,可以通过Spannable来进行修改:
 

01. TextView tv = new TextView(this);
02. tv.setText("字体大小进行设置");
03. //创建Spannable 对象
04. Spannable  span = new SpannableString(tv.getText());
05. /*通过setSpan(Object ,int start,int end,int flag)对特定的内容进行设置
06. 其中 AbsoluteSizeSpan 是决定字体大小
07. */
08. span.setSpan(new AbsoluteSizeSpan(11),0,2,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
09. span.setSpan(new AbsoluteSizeSpan(21),3,6,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
10.  
11. tv.setText(span);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值