Je crée un rectangle dans la méthode @Override
de ReplacementSpan
. Comment ajouter les RoundCorner
et padding
à cela?Comment ajouter coin rond à Drawrect de toile dans Android?
code:
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
RectF rect = new RectF(x, top, x + measureText(paint, text, start, end), bottom);
paint.setColor(mBackgroundColor);
canvas.drawRect(rect, paint);
paint.setColor(mForegroundColor);
canvas.drawText(text, start, end, x, y, paint);
}
Modifier-1
J'utilise measureText:
private float measureText(Paint paint, CharSequence text, int start, int end) { return paint.measureText(text, start, end); }
Modifier-2
Après quelques suggestions que je fait ces changements et je peut voir Rounded
coin sur Rectangle
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
RectF rect = new RectF(x, top, x + measureText(paint, text, start, end), bottom);
paint.setColor(mBackgroundColor);
canvas.drawRoundRect(rect, 15,15,paint);
paint.setColor(mForegroundColor);
canvas.drawText(text, start, end, x, y, paint);
}
et voici la capture d'écran:
J'appelle méthode draw
de code suivant:
int currentIndex = 0;
for (int i = 0; i < words.length - 1; i++) {
s.setSpan(new CustomDrawble(Color.GRAY, Color.WHITE), currentIndex, currentIndex + words[i].length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
currentIndex += words[i].length() + 1;
}
Merci pour votre réponse je viens d'utiliser et il fonctionne ! Je cherchais Comment définir le texte Margin of DrawRect? Comment puis je faire ça? –
marge? La seule chose qui me vient à l'esprit est de rendre le rectangle un peu plus grand. la toile ne s'occupe pas de maring/padding. Vous pouvez essayer le niveau de vue – Blackbelt
Actuellement, j'utilise ReplacementSpan et pour mesurer le texte, j'utilise measureText. S'il vous plaît jeter un oeil à la section Modifier-1 –