
Android TextView自定义字体与描边教程
102KB |
更新于2024-09-01
| 29 浏览量 | 举报
收藏
"Android为TextView添加字体库和设置描边的方法"
在Android开发中,为了提供更丰富的用户体验,有时我们需要自定义TextView的字体样式,包括使用非系统默认的字体库和为文字添加描边效果。本篇文章将详细讲解这两个方面。
一、使用系统自带的字体
虽然Android系统默认提供了三种字体类型:sans(无衬线字体)、serif(衬线字体)和monospace(等宽字体),但这些字体对于中文支持有限。在XML布局文件中,可以通过`typeface`属性来设置字体:
```xml
<TextView
android:id="@+id/sans"
android:text="Hello,World"
android:textSize="20sp"
android:typeface="sans" /> <!-- 使用sans字体 -->
<TextView
android:id="@+id/serif"
android:text="Hello,World"
android:textSize="20sp"
android:typeface="serif" /> <!-- 使用serif字体 -->
<TextView
android:id="@+id/monospace"
android:text="Hello,World"
android:textSize="20sp"
android:typeface="monospace" /> <!-- 使用monospace字体 -->
```
在Java代码中,同样可以设置字体:
```java
TextView textView = findViewById(R.id.textview);
// 设置字体
textView.setTypeface(Typeface.SERIF); // serif字体
textView.setTypeface(Typeface.SANS_SERIF); // sans字体
textView.setTypeface(Typeface.MONOSPACE); // monospace字体
```
二、为TextView添加字体库
当需要使用自定义字体时,需要将字体文件(通常为`.ttf`或`.otf`格式)放入项目的`res/font`目录下,然后创建一个字体家族资源文件(例如`font.xml`):
```xml
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
<font
android:fontStyle="normal"
android:fontWeight="400"
android:font="@font/custom_font" />
</font>
```
在这个例子中,`custom_font`是自定义字体文件的名称。
接着,在XML布局文件中引用这个字体家族:
```xml
<TextView
android:id="@+id/custom_font_textview"
android:text="Hello,World"
android:textSize="20sp"
android:fontFamily="@font/font_family_name" /> <!-- 引用font.xml中定义的字体家族 -->
```
在Java代码中,也可以动态设置字体:
```java
Typeface customFont = ResourcesCompat.getFont(context, R.font.custom_font);
textView.setTypeface(customFont);
```
三、设置TextView的文字描边
实现TextView描边效果通常需要自定义一个`TextView`子类,并重写`onDraw()`方法:
```java
public class StrokedTextView extends TextView {
private int strokeColor = Color.BLACK;
private int strokeWidth = 2;
public StrokedTextView(Context context) {
super(context);
}
public StrokedTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public StrokedTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 创建一个新的画布,用于绘制描边
Canvas strokeCanvas = new Canvas();
Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
strokeCanvas.setBitmap(bitmap);
// 先绘制原始内容
super.onDraw(strokeCanvas);
// 设置画笔
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(strokeColor);
paint.setStrokeWidth(strokeWidth);
// 绘制描边
strokeCanvas.drawRect(0, 0, getWidth(), getHeight(), paint);
// 将描边后的图像绘制到原画布上
canvas.drawBitmap(bitmap, 0, 0, null);
// 释放资源
bitmap.recycle();
}
public void setStrokeColor(int strokeColor) {
this.strokeColor = strokeColor;
invalidate();
}
public void setStrokeWidth(int strokeWidth) {
this.strokeWidth = strokeWidth;
invalidate();
}
}
```
通过这个自定义的`StrokedTextView`,可以在XML布局文件中使用,并设置描边颜色和宽度:
```xml
<com.example.yourpackage.StrokedTextView
android:id="@+id/stroked_textview"
android:text="Hello,World"
android:textSize="20sp"
android:strokeColor="#FF0000" <!-- 描边颜色 -->
android:strokeWidth="3dp" /> <!-- 描边宽度 -->
```
在Java代码中,还可以动态调整描边效果:
```java
StrokedTextView strokedTextView = findViewById(R.id.stroked_textview);
strokedTextView.setStrokeColor(Color.RED); // 设置描边颜色
strokedTextView.setStrokeWidth(5); // 设置描边宽度
```
通过以上方法,开发者可以根据需求自由定制TextView的字体和描边效果,提升应用的视觉体验。
相关推荐








weixin_38584058
- 粉丝: 5
最新资源
- 鑫钥匙免费全功能进销存管理软件
- 深入探究LL(1)算法与Java实现
- 刘振安讲授的Windows可视化程序设计课程
- 掌握Visual C++ 开发GIS系统的高清学习指南
- 掌握s3c2440 LED驱动开发与应用
- Maya插件cvXporter:导出兼容Quest3d的.X文件
- Ethereal网络分析仪中文使用手册
- 检测CPU支持的指令集与技术:MMX、SSE及Hyper-Threading
- 《Begining Linux Programming》第四版:Linux编程学习宝典
- 精选各大公司面试题库及答案解析
- 浙大邹伯敏自动控制理论课件第三版精讲
- ucos内核小模式移植攻略与实践分享
- 基于TCP协议的ChatRoom聊天室客户端与服务器端实现
- 局域网聊天实现:简易socket通信指南
- 掌握VERILOG关键点与易错点学习笔记
- 探索花店管理系统:创新技术与源代码分享
- 华中科技大学《工程测试技术基础》课件精讲
- 《使用裸对象的实用领域驱动设计》PDF版本介绍
- Kstar-1.0版本发布:包含编译包和源码包
- Windows.API编程接口深度解析
- 解锁神器:UNLOCKER_V1.8.7中文版助你删除顽固文件
- 动态演示Windows版数据结构算法教学软件
- 免费分享WEB版SQL Server企业管理器源码
- VanDyke SecureCRT x64 v6.5.2.446 安全终端仿真器