当 TextView 中部分文字需要添加背景时,我们一般使用 BackgroundColorSpan 实现,效果如下图:
为了美化 UI,有时需要实现带圆角的背景,如下图:
这时需要怎么实现呢?首先想到的是查看 BackgroundColorSpan 是否有设置圆角的接口,但不幸的是并没有该接口。接着想到的是是否可以实现一个继承 BackgroundColorSpan 的类,来绘制背景。但查看 BackgroundColorSpan 类,并没有绘制背景的相关接口。
package android.text.style;
import android.annotation.NonNull;
import android.os.Parcel;
import android.text.ParcelableSpan;
import android.text.TextPaint;
public class BackgroundColorSpan extends CharacterStyle implements UpdateAppearance, ParcelableSpan {
public BackgroundColorSpan(int color) {
throw new RuntimeException("Stub!");
}
public BackgroundColorSpan(@NonNull Parcel src) {
throw n