开发者

Multi Formatted TextView

I'm trying to have the layout of the comments on a progam in the following form:

Multi Formatted TextView

I want the USER_NAME in Bold, and the DATE in Enphasis (The red part should be normal, I don't want it red). My problem comes with the red part. I cant get the cooment to be displayed like this using layouts, I've been able to get:

开发者_StackOverflow社区

Multi Formatted TextView

and

Multi Formatted TextView

Is there any way to get the comment working like the one I'm showing. I've thought about a multi formatted textView, is it posible??

If getting the comment like this is not possible, just say it's imposible.


One possible solution is to put two TextViews into a Linear layout one after another and then use SpannableStringBuilder to assign the formatted text to the first view.

SpannableStringBuilder sb = new SpannableStringBuilder();
sb.append(text);
sb.setSpan(createBoldSpan(), 0, lengthOf(userName), 
           Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
firstTextView.setText(sb, TextView.BufferType.NORMAL);

createBoldSpan should return TextAppearanceSpan

private TextAppearanceSpan userNameSpanInBold(String userName) {
    return new TextAppearanceSpan(...);
}

please follow API docs for TextAppearanceSpan - you can create one using custom style.


You could use a WebView with loadData if HTML markup would be good, or alternatively delve into the realm of Spannable text.

I've not worked with Spannable text myself, but essentially it allows you to attach attributes (e.g. styles & colors) to parts of text in a TextView.

The relevant method is TextView.setText(CharSequence text, TextView.BufferType type) with BufferType of SPANNABLE. The documentation I've seen is.... confusing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜