如何显示TextView的里面的html codeS或Android上的其他什么东西? [英] How to show html codes inside TextView or something else on Android?
问题描述
我怎么能显示HTML源代码中的TextView或别的东西呈现?
我想这样的:
How can I show html source as rendered in TextView or something else? I tried this:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView) findViewById(R.id.tv1);
String s = "<img src=\"http://images2.layoutsparks.com/1/146278/purple-lotus-lovely-dew.jpg\"/>";
Spanned sp = new SpannableString(s);
tv.setText(Html.toHtml(sp));
}
但我得到:
But I got:
推荐答案
在一个TextView HTML的支持是pretty有限的,但这里是一些你可以做的事情的例子:
HTML support in a TextView is pretty limited, but here is an example of some of the things you can do:
https://github.com/brianjolly/TextFun
关键是逃避开括号在strings.xml
The key is escaping the opening bracket in your strings.xml
<string name="fun_text">I like <font color=\'0x00b0f1\'>BLUE!</font> and <b> Bold</b> and <i> italic</i> and <u> underline</u>.</string>
然后重新像这样的文字:
And then resetting the text like so:
String styledText = tv.getText().toString();
tv.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE);
开源的好处是,你可以看到什么Html.fromHtml()在这里Android源支持:结果
<一href=\"http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/java/android/text/Html.java;h=07e71f91c927681ea3ab417330678a22fe226b3a;hb=HEAD\" rel=\"nofollow\">http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/java/android/text/Html.java;h=07e71f91c927681ea3ab417330678a22fe226b3a;hb=HEAD
如果你希望做一些更先进看一看的的WebView
If you're looking to do something more advanced take a look at WebView
这篇关于如何显示TextView的里面的html codeS或Android上的其他什么东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!