Вопрос

So I am in a little catch 22 here. I want to format part of my setText and not the other.

I currently have:

workoutInfo.setText(Html.fromHtml("<b>Workout Details: </b><br />") + articleData[6]);

looks like: Workout Details: (notice no bold)

test

test

test

this does not format any text (but keeps the carriage returns container in articleData[6]

If i change the fromHtml to encompass articleData as such:

workoutInfo.setText(Html.fromHtml(("<b>Workout Details: </b><br />") + articleData[6]));

looks like: Workout Details:

test test test (notice no carriage returns)

It correctly formats the html in the first part, however articleData (imported from a db) loses its carriage returns.

Anyone see what I am doing wrong here?

Это было полезно?

Решение

Anyone see what I am doing wrong here?

In the first case, you are removing your HTML formatting by implicitly calling toString() on the results of Html.fromHtml() when you concatenate it with articleData.

In the second case, if articleData truly contains newlines, those are converted into plain spaces as part of normal HTML rendering.

Try using TextUtils.concat() instead of + in your first case, as that should retain the spans created using Html.fromHtml().

Другие советы

Html.fromHtml() returns a Spanned instance. However, concatenating a String converts it to a String. You can easily check this by stepping into with the debugger, and you'll see that the compiler added a StringBuilder to concatenate them. That's why the format is lost in the first alternative.

So the second alternative should be the one to be used. To keep the newlines, just replace them by brs.

String textWithLines = "one\r\ntwo\r\nthree";
edit.setText(Html.fromHtml("<b>Workout Details: </b><br />" + textWithLines.replace("\r\n", "<br>")));
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top