검은 글씨의 흰색 외곽선을 만들어 볼 것이다.
00. 먼저, TextView를 상속받아서 OutlineTextView 클래스를 만들자.
01. round.xml 파일을 만들고 아래 내용을 기입하자.
02. 사용할 레이아웃에다 방금 만든 OutlineTextview 클래스를 적용해보자.(clickable = "true" 를 해줘야 클릭 가능.)
03. 적용된 화면.
***** OutlineTextView Class
package com.astar.smin.ssbw;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.widget.TextView;
public class OutlineTextView extends TextView {
private boolean stroke = false;
private float strokeWidth = 0.0f;
private int strokeColor;
public OutlineTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView(context, attrs);
}
public OutlineTextView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context, attrs);
}
public OutlineTextView(Context context) {
super(context);
}
private void initView(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.OutlineTextView);
stroke = a.getBoolean(R.styleable.OutlineTextView_textStroke, false);
strokeWidth = a.getFloat(R.styleable.OutlineTextView_textStrokeWidth, 0.0f);
strokeColor = a.getColor(R.styleable.OutlineTextView_textStrokeColor, 0xffffffff);
}
@Override
protected void onDraw(Canvas canvas) {
if (stroke) {
ColorStateList states = getTextColors();
getPaint().setStyle(Style.STROKE);
getPaint().setStrokeWidth(strokeWidth);
setTextColor(strokeColor);
super.onDraw(canvas);
getPaint().setStyle(Style.FILL);
setTextColor(states);
}
super.onDraw(canvas);
}
@Override
public boolean callOnClick() {
return super.callOnClick();
}
}
**** round.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="OutlineTextView">
<attr format="boolean" name="textStroke"/>
<attr format="float" name="textStrokeWidth"/>
<attr format="color" name="textStrokeColor"/>
</declare-styleable>
</resources>