[英]Width and Height Custom Textview with stroke not work
I have a custom TextView to set stroke lines, and root layout for this TextView is RelativeLayout with wrap_content, but when i change stroke size, width and height in layout cant adjust with TextView size with stroke, and stroke truncated with layout. 我有一个自定义TextView来设置描边线,并且此TextView的根布局是带有wrap_content的RelativeLayout,但是当我更改描边大小时,布局中的宽度和高度不能随描边的TextView大小进行调整,而描边随布局而被截断。 This is my custom TextView :
这是我的自定义TextView:
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.TextView;
public class TextViewCustom extends TextView {
private static final int DEFAULT_OUTLINE_COLOR = Color.parseColor("#00FFFFFF");
private static final int DEFAULT_OUTLINE_SIZE = 0;
private static final boolean DEFAULT_OUTLINE_STATE = true;
private int outlineColor;
private int outlineSize;
private boolean outlineState;
public TextViewCustom(Context context) {
this(context, null);
}
public TextViewCustom(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public TextViewCustom(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
outlineColor = DEFAULT_OUTLINE_COLOR;
outlineSize = DEFAULT_OUTLINE_SIZE;
outlineState = DEFAULT_OUTLINE_STATE;
}
public int getOutlineColor() {
return outlineColor;
}
public void setOutlineColor(int outlineColor) {
this.outlineColor = outlineColor;
invalidate();
}
public int getOutlineSize() {
return outlineSize;
}
public void setOutlineSize(int outlineSize) {
this.outlineSize = outlineSize;
invalidate();
}
public boolean getOutlineState() {
return outlineState;
}
public void setOutlineState(boolean state) {
outlineState = state;
invalidate();
}
@Override
public void draw(Canvas canvas) {
if (outlineState) {
getPaint().setColor(outlineColor);
getPaint().setStyle(Paint.Style.STROKE);
getPaint().setStrokeWidth(outlineSize);
canvas.save();
canvas.translate(getCompoundPaddingLeft() + outlineSize, getCompoundPaddingTop());
getLayout().draw(canvas);
canvas.restore();
}
getPaint().setColor(getCurrentTextColor());
getPaint().setStyle(Paint.Style.FILL);
canvas.save();
canvas.translate(outlineSize, 0);
super.draw(canvas);
canvas.restore();
}
@Override
public boolean onSetAlpha(int alpha) {
setTextColor(getTextColors().withAlpha(alpha));
setHintTextColor(getHintTextColors().withAlpha(alpha));
setLinkTextColor(getLinkTextColors().withAlpha(alpha));
getBackground().setAlpha(alpha);
return true;
}
}
Override onDraw() method isn't a good way to do TextView with stroke. 重写onDraw()方法不是用笔触制作TextView的好方法。 Instead of you should use xml and set it as background of TextView.
而不是您应该使用xml并将其设置为TextView的背景。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="1dp"
android:color="your stoke color" />
<solid android:color="your background color" />
</shape>
And in layout 并在布局中
<TextView ... android:background="@drawable/your xml file" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.