控制不同的文字字体

TextView对象中有许多与字形相关的方法,使用setTextSize方法来改变字体大小,用setTypeface方法来指定使用字体等等。
如果你想使用内部默认的Typeface,用defaultFromStyle()方法即可。但是,如果你想要通过外部的资源来构造Typeface,步骤如下:
1. 事先在assets目录下创建一个fonts文件夹
2. 放入要使用的字体文件(.ttf)
3. 提供相对路径给createFromAsset()来创建Typeface对象
使用外部Typeface如下:
eg.
textview.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/HandmadeTypewriter.ttf"));

使用内部Typeface,如下:
 website.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));

完整代码:
package com.kevin.textview;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.TextView;

public class TextViewActivity extends Activity {
private TextView website, email, phone;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
website = (TextView) findViewById(R.id.tv_website);
email = (TextView)findViewById(R.id.tv_email);
phone = (TextView) findViewById(R.id.tv_phone);
// 设置文本值
website.setText(R.string.website);
email.setText(R.string.email);
phone.setText(R.string.phone);

// 设置字体大小
website.setTextSize(20);
// 设置字体
/*
* 使用内部默认的Typeface,用defaultFromStyle()方法
* 如果你想要通过外部的资源来构造Typeface,步骤如下:
* 1. 事先在assets目录下创建一个fonts文件夹
* 2. 放入要使用的字体文件(.ttf)
* 3. 提供相对路径给createFromAsset()来创建Typeface对象
*/
website.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值