android 字体总结
16lz
2021-12-04
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));
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));
更多相关文章
- 传智播客Android视频教程——第四天
- Android(安卓)常用工具
- android 根据string 获取资源文件的id
- Android(安卓)NDK 的学习之旅-----JNI LOG 打印
- Android(安卓)实例子源代码文件下载地址380个合集
- android--RelativeLayout布局的一些理解
- android adb shell 命令大全
- android 布局文件中xmlns:android="http://schemas.android.com/
- Android(安卓)资源文件中@、@android:type、@*、?、@+含义和区别