[英]Border in Clickable object in SpannableString
目前我有一個SpannableString對象,其中設置了多個Clickable對象。 因此,一個字符串包含許多可點擊對象,具體取決於用戶點擊該應用程序的單詞/部分,並繼續處理該點擊事件。 前幾天我在stackoverflow上問過關於在SpannableString中刪除部分單詞的藍色下划線,答案是對ClickableSpan類的子類,並覆蓋updateDrawState方法並將underlineText設置為false。
我的問題:是否可以在SpannableString中的Clickable對象周圍放置邊框? 所以基本上每個Clickable對象/字符串都必須有自己的邊框。
我想也許updateDrawState方法可能能夠提供幫助,但事實並非如此。 有誰知道如何實現這一目標?
謝謝。
我擴展了ReplacementSpan
以制作概述范圍。 不幸的是, 我無法讓它們換行 ,但是如果你只是想把你的大綱應用到幾個單詞,它應該可以正常工作。 為了使這個可單擊,你可以在設置這個之前使用你提到的setSpan(ClickableSpanWithoutUnderline...)
的子類。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_replacement_span);
final Context context = this;
final TextView tv = (TextView) findViewById(R.id.tv);
Spannable span = Spannable.Factory.getInstance().newSpannable("Some string");
span.setSpan(new BorderedSpan(context), 0, span.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(span, TextView.BufferType.SPANNABLE);
}
public static class BorderedSpan extends ReplacementSpan {
final Paint mPaintBorder, mPaintBackground;
int mWidth;
Resources r;
int mTextColor;
public BorderedSpan(Context context) {
mPaintBorder = new Paint();
mPaintBorder.setStyle(Paint.Style.STROKE);
mPaintBorder.setAntiAlias(true);
mPaintBackground = new Paint();
mPaintBackground.setStyle(Paint.Style.FILL);
mPaintBackground.setAntiAlias(true);
r = context.getResources();
mPaintBorder.setColor(Color.RED);
mPaintBackground.setColor(Color.GREEN);
mTextColor = Color.BLACK;
}
@Override
public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
//return text with relative to the Paint
mWidth = (int) paint.measureText(text, start, end);
return mWidth;
}
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
canvas.drawRect(x, top, x + mWidth, bottom, mPaintBackground);
canvas.drawRect(x, top, x + mWidth, bottom, mPaintBorder);
paint.setColor(mTextColor); //use the default text paint to preserve font size/style
canvas.drawText(text, start, end, x, y, paint);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.