我在android中遇到自定义文本视图组件的问题。我使用自定义spannable来设置textview的背景,如第一张图片中所示。这适用于android api level 21(第一张图片)。但是如果我使用api level 25(第二张图片),自定义textview就会消失。
自定义文字视图中的代码
public class MetalTextView extends android.support.v7.widget.AppCompatTextView
{
private final String xmlns = "http://schemas.android.com/apk/res/android";
private Context context;
private AttributeSet attributeSet;
private String value;
public MetalTextView(Context context)
{
super(context);
this.context = context;
value = attributeSet.getAttributeValue(xmlns, "text");
}
public MetalTextView(Context context, @Nullable AttributeSet attrs)
{
super(context, attrs);
this.context = context;
this.attributeSet = attrs;
value = attributeSet.getAttributeValue(xmlns, "text");
}
public MetalTextView(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
this.context = context;
this.attributeSet = attrs;
value = attributeSet.getAttributeValue(xmlns, "text");
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
final Spannable spannable = new SpannableString(value);
spannable.setSpan(new RoundedBackgroundSpannable(), 0, value.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
this.setText(spannable);
}
@Override
public void setText(CharSequence text, BufferType type)
{
super.setText(text, type);
value = text.toString();
final Spannable spannable = new SpannableString(text);
spannable.setSpan(new RoundedBackgroundSpannable(), 0, value.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
来自我的客户的代码spannable
public class RoundedBackgroundSpannable extends ReplacementSpan
{
private static int CORNER_RADIUS = 20;
private final int backgroundColor = 0xFFFFFFFF;
private final int textColor = 0xFF000000;
private final int borderColor = 0xFF000000;
private final float strokeWidth = 5f;
public RoundedBackgroundSpannable()
{
super();
}
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint)
{
final RectF rectF = new RectF(x, top, x + measureText(paint, text, start, end ), bottom);
paint.setColor(backgroundColor);
canvas.drawRoundRect(rectF, CORNER_RADIUS, CORNER_RADIUS, paint);
paint.setColor(textColor);
canvas.drawText(text, start, end, x, y, paint);
paint.setColor(borderColor);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(strokeWidth);
final Path path = new Path();
path.addRoundRect(rectF, CORNER_RADIUS, CORNER_RADIUS, Path.Direction.CW);
canvas.drawPath(path, paint);
}
@Override
public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm)
{
return Math.round(paint.measureText(text, start, end));
}
private float measureText(Paint paint, CharSequence text, int start, int end)
{
return paint.measureText(text, start, end);
}
}
我对自定义组件的使用
<com.metalcrunch.ui.MetalTextView
android:id="@+id/metalTextView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="@dimen/textview_headline_margin_top"
android:layout_weight="0.1"
android:gravity="center"
android:text="Some Text"
android:textSize="@dimen/textview_headline_text_size"/>
textview在api 25级消失的原因是什么?