text——android.graphic.typeface字体类

本文介绍如何在Android中使用不同字体类型和风格,并提供通过XML属性和代码方式设置字体的具体方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自http://www.cnblogs.com/Gaojiecai/archive/2012/05/06/2485541.html

本类的常量静态定义

字体类型(typeface)名称

Typeface DEFAULT//<span style="font-size:10.5pt"><span style="font-size:10.5pt">默认字体</span></span>
Typeface DEFAULT_BOLD//<span style="font-size:10.5pt"><span style="font-size:10.5pt">默认粗体</span></span>
Typeface MONOSPACE//<span style="font-size:10.5pt">单间隔字体</span>
Typeface SANS_SERIF//<span style="font-size:10.5pt">无衬线字体</span>
Typeface SERIF//<span style="font-size:10.5pt">衬线字体</span>

字体风格(style)名称

int BOLD//<span style="font-size:10.5pt">粗体</span>
int BOLD_ITALIC//<span style="font-size:10.5pt">粗斜体</span>
int ITALIC//<span style="font-size:10.5pt">斜体</span>
int NORMAL//<span style="font-size:10.5pt">普通字体</span>

设置TextView的字体可以通过TextView中的setTypeface方法来指定一个Typeface对象 因为Android的字体类 比较简单,我们列出所有成员方法:

//静态方法,参数一为字体类型这里是Typeface的静态定义,如宋体,参数二风格,如粗体,斜体
static Typeface create(Typeface family, int style)

//静态方法,参数一为字体名的字符串,参数二为风格同上,这里我们推荐使用上面的方法。
static Typeface create(String familyName, int style)

//静态方法,参数一为AssetManager对象,主要用于从APK的assets文件夹中取出字体,参数二为相对于Android工程下的assets文件夹中的外挂字体文件的路径。

//静态方法,从文件系统构造一个字体,这里参数可以是sdcard中的某个字体文件
static Typeface createFromFile(File path)

//静态方法,从指定路径中构造字体
static Typeface createFromFile(String path) 

//静态方法,返回默认的字体风格
static Typeface defaultFromStyle(int style) 

//获取当前字体风格
int getStyle() 

//判断当前是否为粗体
final boolean isBold() 

//判断当前风格是否为斜体
final boolean isItalic() 


例程:

 <!--  使用默认的sans字体-->
<TextView    
android:id="@+id/sans"
android:text="Hello,World"
android:typeface="sans"
android:textSize="20sp">
</TextView>

<!--  使用默认的serifs字体-->
<TextView    
android:id="@+id/serif"
android:text="Hello,World"
android:typeface="serif"
android:textSize="20sp">
</TextView>

 <!--  使用默认的monospace字体-->
<TextView    
android:id="@+id/monospace"
android:text="Hello,World"
android:typeface="monospace"
android:textSize="20sp">
</TextView>


代码中设置字体


 //得到TextView控件对象
  TextView textView = (TextView)findViewById(R.id.custom);

 //将字体文件保存在assets/fonts/目录下,创建Typeface对象
   Typeface typeFace = Typeface.createFromAsset(getAssets(), "fonts/HandmadeTypewriter.ttf");

   //应用字体
  textView.setTypeface(typeFace);









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值