字符串资源中的HTML?

Fel*_*lix 114 android

我知道我可以将转义的HTML标记放在字符串资源中.但是,查看Contacts应用程序的源代码,我可以看到他们有一种不必编码HTML的方法.从Contacts应用程序strings.xml中引用:

<string name="contactsSyncPlug"><font fgcolor="#ffffffff">Sync your Google contacts!</font> 
\nAfter syncing to your phone, your contacts will be available to you wherever you go.</string>
Run Code Online (Sandbox Code Playgroud)

不幸的是,当我尝试类似的东西(比如Hello, <b>World</b>!)时,getString()返回没有标签的字符串(我可以看到logcat).这是为什么?如何获得带有标签和所有内容的原始字符串?联系人应用程序如何执行此操作?

Don*_*ker 181

你也可以将你的html包围在CDATA块中,getString将返回你的实际HTML.像这样:

<string name="foo"><![CDATA[Foo Bar <a href="foo?id=%s">baz</a> is cool]]></string>
Run Code Online (Sandbox Code Playgroud)

现在当你执行getString(R.string.foo)时,字符串将是HTML.如果需要通过可点击的TextView呈现HTML(带有如图所示的链接),则需要执行Html.fromHtml(...)调用以获取可跨越文本.

  • @MarcoW.Felix的答案是正确的,但使用CDATA可以帮助我们不用担心html标签.这个答案应该是正确答案. (4认同)
  • 如果您在字符串中有链接,请不要忘记添加textView.setMovementMethod(LinkMovementMethod.getInstance()); (3认同)

Fel*_*lix 88

它似乎getString()只是 - 得到一个字符串.要使用它,你必须使用getText()(而不是更多Html.fromHtml()),即:

mTextView.setText(getText(R.string.my_styled_text));
Run Code Online (Sandbox Code Playgroud)

但是,看起来android:text属性也是一样的,以下是等价的:

<TextView android:text="@string/my_styled_text" />
Run Code Online (Sandbox Code Playgroud)

并在strings.xml:

<string name="my_styled_text">Hello, <b>World</b>!</string>
Run Code Online (Sandbox Code Playgroud)

  • 请注意,只有支持的标签是<b>,<i>,<u>:http://developer.android.com/guide/topics/resources/string-resource.html (23认同)
  • @Snicolas:它支持的不仅仅是文档中提到的3个标签:它支持<b>,<i>,<u>,<big>,<small>,<sup>,<sub>,<strike>, <li>,<marquee>,<a>,<font>和<annotation>(请参阅https://github.com/android/platform_frameworks_base/blob/android-4.2.2_r1/core/java/android/content/res /StringBlock.java#L161) (7认同)
  • 你的意思是getText():) (2认同)
  • @pawegio肯定你的意思是`\n`? (2认同)

小智 49

最好的解决方案是以某种方式使用资源:

<string name="htmlsource"><![CDATA[<p>Adults are spotted gold and black on the crown, back and wings. Their face and neck are black with a white border; they have a black breast and a dark rump. The legs are black.</p><p>It is similar to two other golden plovers, Eurasian and Pacific. <h1>The American Golden Plover</h1> is smaller, slimmer and relatively longer-legged than Eurasian Golden Plover (<i>Pluvialis apricaria</i>) which also has white axillary (armpit) feathers. It is more similar to Pacific Golden Plover (<i>Pluvialis fulva</i>) with which it was once <b>considered</b> conspecific under the name \"Lesser Golden Plover\". The Pacific Golden Plover is slimmer than the American species, has a shorter primary projection, and longer legs, and is usually yellower on the back.</p><p>These birds forage for food on tundra, fields, beaches and tidal flats, usually by sight. They eat insects and crustaceans, also berries.</p>]]></string>
Run Code Online (Sandbox Code Playgroud)

并显示它:

Spanned sp = Html.fromHtml( getString(R.string.htmlsource));
tv.setText(sp);
Run Code Online (Sandbox Code Playgroud)

尝试在没有和使用tv.setText的情况下使用该资源(getText(R.string.htmlsource)); 你会看到差异.