2012-11-04 47 views
2

如何在我的textView的文字周围制作黑线?例如上述图像围绕文字的黑线

enter image description here

+0

我不明白,MS油漆? PhotoShop的?瘸子? –

+0

尝试在它后面放置一个黑色阴影:http://stackoverflow.com/questions/3182393/android-textview-outline-text – Ahmad

+0

@DannyHong我认为Max想勾勒出他的文字。 – Ahmad

回答

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)。

+0

非常感谢你 –

+0

不客气。 – Simon

0

描述一下添加到您的xml文件的字体:

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