Android的TextView的文字背景颜色颜色、背景、文字、Android

2023-09-05 00:36:15 作者:温柔童话集

我怎样才能实现与一个Android的TextView这样的效果。它看起来像某种方式选定的文本,我找不到API中类似的事情。

How can I achieve such an effect with an Android TextView. It looks somehow like selected text and I couldn't find something similar in the API.

这不是一个背景色视图,但背景颜色只为文本。你可以看到它是如何停止在换行,并具有文本行之间的细白线。

This is not a background color for the view, but a background color only for the text. You can see how it stops at line breaks and has a thin white line between text lines.

推荐答案

据我所看到的,就是这样做并没有压倒一切的TextView以及包括间隙颜色的观点绘制自定义的油漆没有'好'的方式。

As far as I can see, there's no 'nice' way of doing this without overriding TextView and drawing custom paints on the view which includes the gap colour.

即使设置 lineSpacingExtra 属性只扩大的背景颜色。

Even setting the lineSpacingExtra property only expands the background colour.

您也可能会考虑创建一个自定义的 spannable 并使用它像

You could also potentially look into creating a custom spannable and use it like

Spannable str = new SpannableStringBuilder("How can I achieve such an effect with an Android TextView. It looks somehow like selected text and I couldn't find something similar in the API.");
str.setSpan(new NewSpannableClass(), 0, str.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
((TextView)findViewById(R.id.text)).setText(str);

其中, NewSpannableClass 是自定义spannable。

Where NewSpannableClass is the custom spannable.