2012-11-04 45 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"