扩展TextView类.然后在onDraw中,首先使用黑色绘制文本,然后再次绘制,略小并使用白色.要获得额外的"正确性",请将自定义属性添加到XML以设置"线条"颜色.
public class myTextView extends TextView{
public myTextView (Context context) {
this(context, null);
}
public myTextView (Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public myTextView (Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// do extra initialisation and get attributes here
}
@Override
protected void onDraw(Canvas canvas) {
// draw first in black
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(20); // text size
paint.setStyle(Paint.Style.STROKE);
paint.setTextAlign(Paint.Align.CENTER);
canvas.drawText("My text", 50, 50, paint);
// draw again in white, slightly smaller
paint.setColor(Color.WHITE);
paint.setTextSize(18); // text size
canvas.drawText("My text", 50, 50, paint);
}
}
Run Code Online (Sandbox Code Playgroud)
代码不完整,因为它硬编码颜色,大小和位置,但我希望它足以让你使用.您可以通过构造函数中的attrs从XML获取文本大小和文本颜色,并将线条颜色和宽度添加为自定义属性(在此处搜索或Google).