Thật sự tôi thấy vấn đề lớn với tất cả những câu trả lời khi hiển thị nhiều dòng phù hiệu. Sau nhiều thử nghiệm và tinh chỉnh. Cuối cùng tôi đã nhận được phiên bản tốt nhất ở trên.
Ý tưởng cơ bản là đánh lừa TextView bằng cách đặt kích thước văn bản lớn hơn nhiều và đặt kích thước mong muốn trong khoảng. Ngoài ra, bạn có thể thấy tôi đang vẽ nền huy hiệu và văn bản khác nhau.
Vì vậy, đây là RoundedBackgroundSpan tôi:
public class RoundedBackgroundSpan extends ReplacementSpan {
private static final int CORNER_RADIUS = 12;
private static final float PADDING_X = GeneralUtils.convertDpToPx(12);
private static final float PADDING_Y = GeneralUtils.convertDpToPx(2);
private static final float MAGIC_NUMBER = GeneralUtils.convertDpToPx(2);
private int mBackgroundColor;
private int mTextColor;
private float mTextSize;
/**
* @param backgroundColor color value, not res id
* @param textSize in pixels
*/
public RoundedBackgroundSpan(int backgroundColor, int textColor, float textSize) {
mBackgroundColor = backgroundColor;
mTextColor = textColor;
mTextSize = textSize;
}
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
paint = new Paint(paint); // make a copy for not editing the referenced paint
paint.setTextSize(mTextSize);
// Draw the rounded background
paint.setColor(mBackgroundColor);
float textHeightWrapping = GeneralUtils.convertDpToPx(4);
float tagBottom = top + textHeightWrapping + PADDING_Y + mTextSize + PADDING_Y + textHeightWrapping;
float tagRight = x + getTagWidth(text, start, end, paint);
RectF rect = new RectF(x, top, tagRight, tagBottom);
canvas.drawRoundRect(rect, CORNER_RADIUS, CORNER_RADIUS, paint);
// Draw the text
paint.setColor(mTextColor);
canvas.drawText(text, start, end, x + PADDING_X, tagBottom - PADDING_Y - textHeightWrapping - MAGIC_NUMBER, paint);
}
private int getTagWidth(CharSequence text, int start, int end, Paint paint) {
return Math.round(PADDING_X + paint.measureText(text.subSequence(start, end).toString()) + PADDING_X);
}
@Override
public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
paint = new Paint(paint); // make a copy for not editing the referenced paint
paint.setTextSize(mTextSize);
return getTagWidth(text, start, end, paint);
}
}
Và đây là làm thế nào tôi đang sử dụng nó:
public void setTags(ArrayList<String> tags) {
if (tags == null) {
return;
}
mTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 26); // Tricking the text view for getting a bigger line height
SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
String between = " ";
int tagStart = 0;
float textSize = 13 * getResources().getDisplayMetrics().scaledDensity; // sp to px
for (String tag : tags) {
// Append tag and space after
stringBuilder.append(tag);
stringBuilder.append(between);
// Set span for tag
RoundedBackgroundSpan tagSpan = new RoundedBackgroundSpan(bgColor, textColor, textSize);
stringBuilder.setSpan(tagSpan, tagStart, tagStart + tag.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// Update to next tag start
tagStart += tag.length() + between.length();
}
mTextView.setText(stringBuilder);
}
Lưu ý:
- Bạn có thể chơi với tất cả các kích thước và hằng số để phù hợp với thứ bạn muốn YLE
- Nếu bạn sử dụng một phông chữ bên ngoài hãy chắc chắn để thiết lập android: includeFontPadding = "false" nếu không nó có thể lộn xộn lên chiều cao của dòng
Enjoy :)
đăng một hình ảnh những gì bạn muốn làm – pskink
uy tín của tôi là không đủ cao ... – ericlokness
để đăng liên kết? – pskink