文字周围的黑线

时间:2012-11-04 16:31:37

标签: android text

如何在textView的文本周围形成黑线?上图中的示例

enter image description here

3 个答案:

答案 0 :(得分:5)

扩展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);


    }


}

代码不完整,因为它硬编码颜色,大小和位置,但我希望它足以让你使用。您可以通过构造函数中的attrs从XML获取文本大小和文本颜色,并将线条颜色和宽度添加为自定义属性(在此处搜索或使用Google)。

答案 1 :(得分:4)

我会使用已经有http://www.dafont.com/steelfish.font

等大纲的字体

如此处所述Android - Using Custom Font

答案 2 :(得分:1)

将其添加到您的xml文件中:

android:shadowColor="#000000"
android:shadowDx="1.5"
android:shadowDy="1.3"
android:shadowRadius="1.6"
android:text="YOUR TEXT"
android:textColor="@color/white"
相关问题