android 字体总结

Android提供三种字体:“Sans”,“serif“和“monospace”。 默认:Sans

1、在Android XML文件中设置字体

<TextView
android:id="@+id/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello_world!您好,世界!(sans)"
android:typeface="sans" />

<TextView
android:id="@+id/t2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello_world!您好,世界!(serif)"
android:typeface="serif" />

<TextView
android:id="@+id/t3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello_world!您好,世界!(monospace)"
android:typeface="monospace" />

2、自定义字体
  1)将新字体的TTF文件copy到assets/fonts/目录下面。
  2)我们需要将widget设置为该字体,比较遗憾的是,不能直接在XML文件中进行,需要编写源代码。

typeface= Typeface.createFromAsset(getAssets(), "fonts/lz.ttf");

textView.setTypeface(typeface);

3. UI所有组件使用自定义字体

3.1 方法
public static void changeFonts(ViewGroup root, Activity act) {
Typeface tf = Typeface.createFromAsset(act.getAssets(),
"fonts/lz.ttf");
for (int i = 0; i < root.getChildCount(); i++) {
View v = root.getChildAt(i);
if (v instanceof TextView) {
((TextView) v).setTypeface(tf);
} else if (v instanceof Button) {
((Button) v).setTypeface(tf);
} else if (v instanceof EditText) {
((EditText) v).setTypeface(tf);
} else if (v instanceof ViewGroup) {
changeFonts((ViewGroup) v, act);
}
}
}
}
3.2 activity中调用方法

ViewGroup viewGroup=(ViewGroup)findViewById(android.R.id.content);
FontManager.changeFonts(viewGroup, this);

4. html的使用

String str = "测试<b>黑体字</b>、<i>斜体字</i>、<u>下划 线</u>、<font color='red'>红色字</font>的显示。" ;

textView.setText(Html.fromHtml(str));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值