围绕文本的黑线 [英] black line around the text
问题描述
如何让周围的文字对我TextView的一条黑线?例如上面的图片
how to make a black line around the text for my textView? example on above image
推荐答案
扩展的TextView类。然后在的onDraw,绘制文本首先使用黑色,然后再绘制它,更小,而且使用白色。对于额外的正确性,添加自定义属性的XML设置线路围绕色彩。
Extend the TextView class. Then in the onDraw, draw the text first using black, then draw it again, slightly smaller and using white. For extra "correctness", add custom attributes to the XML to set the "line around" colour.
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);
}
}
在code是不完整的,因为它很难codeS的颜色,大小和位置,但我希望这是足以让你一起工作。您可以通过在构造函数中的attrs您可以通过XML文本大小和文本颜色,并添加线条颜色和宽度自定义属性(这里搜索或谷歌)。
The code is not complete as it hardcodes the colours, size and positions but I hope that it is enough for you to work with. You can get the text size and text colour from the XML via attrs in the constructor and add line colour and width as custom attributes (search here or Google).
这篇关于围绕文本的黑线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!