TextView的HTML格式 [英] HTML formatting for TextView

查看:60
本文介绍了TextView的HTML格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于TextView元素是否以格式化形式显示文本的规则",我有些困惑.

I am a bit confused about the 'rules' of when a TextView element displays text in formatted form or not.

像这样的字符串

"There are <i>different ways</i> of coding.\n";

我编写代码时显示时没有任何格式(包括HTML代码)

displays without any formatting (including the HTML codes) when I code

tvMyTextView.setText("There are <i>different ways</i> of coding.\n");

但是当我在strings.xml中定义相同的字符串然后加载

but when I define the same string in strings.xml and then load

tvMyTextView.setText(R.strings.TestString);

显示强调.

尝试将URL嵌入TextView时,我感到更加困惑:

Even more confused I feel when trying to embed URLs in TextView's like here:

"Click <a href="http://www.poon-world.com">here</a> to switch on the red light.\n";

不用说我已经尝试了TextView的各种属性选项-但是除非我错过了一些东西,否则它们似乎并没有太大的区别.在某些情况下,URL以蓝色编码在文本中,可以单击,在其他情况下,我可以看到HTML格式.在其他情况下,它仍然是颜色编码的,并且URL似乎以某种方式被编码在文本中-但是,当我单击它时,什么也没有发生.关于URL的嵌入,与其他具有简单" HTML格式的示例不同,我什至无法找到规则,无论何时该规则起作用,什么时候该规则不起作用.谁能帮我解开脑海中的烦恼..

Needless to say I already tried the various property options of TextView - but they don't seem to make much of a difference unless I missed something. In some cases the URL is encoded in the text, in blue color and can be clicked, in others I can see the HTML formatting. In others again, it is color-encoded and the URL seems to be encoded in the text somehow - but nothing happens when I click it. Regarding the embedding of URLs, unlike for the other example with 'simple' HTML formatting, I couldn't even find out a rule so far of when it works and when it doesn't. Can anyone help me to untie the knots in my head..

推荐答案

实际上,来自Android文档.

Actually, From the Android Docs..

public final void setText (CharSequence text)

设置TextView的字符串值. TextView不接受类似HTML的格式,您可以使用XML资源文件中的文本字符串进行格式化.要为字符串设置样式,请将android.text.style.*对象附加到SpannableString,或参阅可用资源类型"文档以获取在XML资源文件中设置格式化文本的示例.

Sets the string value of the TextView. TextView does not accept HTML-like formatting, which you can do with text strings in XML resource files. To style your strings, attach android.text.style.* objects to a SpannableString, or see the Available Resource Types documentation for an example of setting formatted text in the XML resource file.

但是

public final void setText (int resid)

  • 对此没有更多说明.
  • 但是来自Android Resource String文档.

    But from Android Resource String docs..

    您可以使用HTML标记在字符串中添加样式.例如:

    You can add styling to your strings with HTML markup. For example:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="welcome">Welcome to <b>Android</b>!</string>
    </resources>
    

    受支持的HTML元素包括:

    Supported HTML elements include:

    <b> for bold text.
    <i> for italic text.
    <u> for underline text.
    

    有时您可能需要创建样式化的文本资源,该资源也可用作格式字符串.通常,这是行不通的,因为String.format(String, Object...)方法将从字符串中删除所有样式信息.解决方法是使用转义的实体编写HTML标记,然后在格式化后使用fromHtml(String)恢复这些.

    Sometimes you may want to create a styled text resource that is also used as a format string. Normally, this won't work because the String.format(String, Object...) method will strip all the style information from the string. The work-around to this is to write the HTML tags with escaped entities, which are then recovered with fromHtml(String), after the formatting takes place.

    关于您的URL字符串,...

    And about your URL string,...

    tvMyTextView.setText(Html.fromHtml("Click <a href="http://www.poon-world.com">here</a> to switch on the red light.\n"));
    

    还要查看此问题设置TextView文本从XML的html格式的字符串资源中获取

    Android字符串资源

    这篇关于TextView的HTML格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆