使用值/字符串格式化TextView文本。xml、HTML和CSS

时间:2023-01-27 20:25:52

I am trying to format a TextView with lots of text to look better, and since I feel this is very easy with HTML and CSS I thought I'd give it a go. And since having it in values/strings.xml lets you localize, I found this to be the best option. Now, I manage to get HTML in a string fine with:

我正在尝试用大量的文本来格式化一个TextView,因为我觉得用HTML和CSS很容易,所以我想尝试一下。因为它在值/字符串中。xml允许本地化,我发现这是最好的选择。现在,我设法将HTML放在一个字符串中

<![CDATA[ <html code here> ]]>

But if I try for example:

但如果我试着举例:

<![CDATA[
    <html>
    <head>
    <body>
    <style type=\"text/css\">
        p {
        display: block;
        border-bottom: 1px solid #31B6E7; }
    </style>
    </head>
    ...
    etc
]]>

It will just paste the CSS code as plain text, is it not possible to format using CSS this way or does anyone have a good tip for me here?

它只是将CSS代码粘贴为纯文本,难道不可能用这种方式格式化CSS吗?

Thanks for any help on this one.

谢谢你的帮助。

3 个解决方案

#1


5  

You have to tell the TextView that it is receiving HTML. I think you can find you answer here. That goes for HTML. CSS is not supported - you have to do it via your layout XML...

你必须告诉TextView它正在接收HTML。我想你可以在这里找到答案。HTML。不支持CSS—您必须通过XML布局来实现它。

#2


6  

I think you're perhaps expecting a little much from the TextView, or rather, from the Html.fromHtml method, since that is what turns the HTML into a Spannable that you can use with the TextView. It's quite basic. Here's what it supports:

我认为您可能期望从TextView,或者更确切地说,从HTML . fromhtml方法中得到一些东西,因为这将使HTML变成一个可以与TextView一起使用的Spannable。这是很基本的。这就是它支持:

http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html

http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html

#3


1  

You could use android.webkit.WebView.loadData() to render the HTML + CSS, instead of a TextView.

您可以使用android.webkit.WebView.loadData()来呈现HTML + CSS,而不是TextView。

#1


5  

You have to tell the TextView that it is receiving HTML. I think you can find you answer here. That goes for HTML. CSS is not supported - you have to do it via your layout XML...

你必须告诉TextView它正在接收HTML。我想你可以在这里找到答案。HTML。不支持CSS—您必须通过XML布局来实现它。

#2


6  

I think you're perhaps expecting a little much from the TextView, or rather, from the Html.fromHtml method, since that is what turns the HTML into a Spannable that you can use with the TextView. It's quite basic. Here's what it supports:

我认为您可能期望从TextView,或者更确切地说,从HTML . fromhtml方法中得到一些东西,因为这将使HTML变成一个可以与TextView一起使用的Spannable。这是很基本的。这就是它支持:

http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html

http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html

#3


1  

You could use android.webkit.WebView.loadData() to render the HTML + CSS, instead of a TextView.

您可以使用android.webkit.WebView.loadData()来呈现HTML + CSS,而不是TextView。