У меня есть строка, которую нужно визуализировать в Webview, но у меня возникают проблемы.
Строка выглядит следующим образом.
History
Riots and political conflicts of the 1960s
--------Line Feed1----------------
The LAPD Metropolitan Division's "D" Platoon is one of the world's most prominent SWAT units and was the second SWAT team established in the United States, after that of the Philadelphia Police Department in 1964.[2]
According to the Historical Dictionary of Law Enforcement, the term "SWAT" was used as an acronym for the "Special Weapons and Tactics" established as a 100-man specialized unit in 1964 by the Philadelphia Police Department in response to an alarming increase in bank robberies.
--------Line Feed2----------------
The purpose of this unit was to react quickly and decisively to bank robberies while they were in progress, by utilizing a large number of specially trained officers who had at their disposal a great amount of firepower. The tactic worked and was later soon to resolve other types of incidents involving heavily armed criminals.[2][3] Los Angeles Police Department (LAPD) Inspector Daryl Gates has said that he first envisioned "SWAT" as an acronym for "Special Weapons Attack Team" in 1967, but later accepted "Special Weapons and Tactics" on the advice of his deputy chief, Edward M. Davis.[4]
<p> </p>
<p><span><img src="https://upload.wikimedia.org/wikipedia/commons/1/19/Flag_of_Andorra.svg" border="0" alt="" /></span></p>
<p> </p>
Подход 1:
Когда я передаю выше String для веб-просмотра, используя (str - строка, упомянутая выше)
webview.loadDataWithBaseURL(null,str,"text/html","utf-8",null)
Результат показан на рисунке А ниже. Проблема заключается в том, что символы перевода строки (\ n) и пробелы удаляются веб-представлением.
Подход 2:
Когда я передаю выше String для веб-просмотра, используя
webview.loadDataWithBaseURL(null,Html.toHtml(SpannableString(str)),"text/html","utf-8",null)
Результат, как показано на рисунке B ниже. Проблема в том, что символы перевода строки (\ n) и пробелы отображаются правильно, но HTML также отображается в виде простого текста.
Подход 3:
Когда я передаю выше String для веб-просмотра, используя
webview.loadDataWithBaseURL(null,str.replace("\n",<br/>"),"text/html","utf-8",null)
Результат показан на рисунке С ниже. Проблема здесь в том, что символы перевода строки (\ n) и HTML отображаются правильно, но пробелы в начале абзацев не отображаются.
Мое требование - достичь этого, как показано на рисунке D ниже
Какое было бы оптимальное решение для достижения желаемого результата.
Я создал образец приложения здесь для справки.