html.fromHtml 通过 URl 显示图片和文字

{

    "news_content": [

                "<p<table class=\"tplCaption\" style=\"padding-left: 30px;\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\" align=\"center\">\r\n<tbody>\r\n<tr>\r\n<td><img src=\"https://i-vnexpress.vnecdn.net/2019/06/06/trump-1571-1559784498.jpg\" alt=\"Tổng thống Mỹ Donald Trump. Ảnh: AP.\" data-natural-h=\"334\" data-natural-width=\"500\" data-width=\"500\" data-pwidth=\"500\" /></td>\r\n</tr>\r\n<tr>\r\n<td>\r\n<p class=\"Image\">Tổng thống Mỹ Donald Trump. Ảnh:&nbsp;<em>AP</em>.</p>\r\n</td>\r\n</tr>\r\n</tbody>\r\n</table>\r\n",

                "<p class=\"Normal\">\"Th&agrave;nh thật m&agrave; n&oacute;i t&ocirc;i chưa bao giờ l&agrave; người ủng hộ cuộc chiến đ&oacute;. T&ocirc;i nghĩ cuộc chiến đ&oacute; rất tồi tệ. T&ocirc;i nghĩ Việt Nam ở rất xa v&agrave; khi đ&oacute; &iacute;t người biết về đất nước n&agrave;y\", Tổng thống Mỹ Donald Trump ng&agrave;y 5/6 đề cập đến cuộc chiến tranh ở Việt Nam khi trả lời phỏng vấn nh&agrave; b&aacute;o Anh Piers Morgan về việc liệu &ocirc;ng c&oacute; thể v&agrave; c&oacute; muốn phục vụ trong qu&acirc;n đội hay kh&ocirc;ng.</p>\r\n<p class=\"Normal\">C&acirc;u trả lời của Tổng thống Trump phần n&agrave;o phản &aacute;nh quan điểm của người d&acirc;n Mỹ đối với cuộc chiến phi nghĩa ở Việt Nam v&agrave;o thập ni&ecirc;n 1960 v&agrave; 1970, khi nhiều cuộc biểu t&igrave;nh nổ ra tr&ecirc;n khắp nước Mỹ phản đối việc đưa lực lượng qu&acirc;n sự tới can thiệp ở quốc gia Đ&ocirc;ng Nam &Aacute; n&agrave;y.</p>\r\n",

                "<p class=\"Normal\"><strong>Huyền L&ecirc;</strong>&nbsp;(Theo&nbsp;<em>CNN</em></p>"

            ],

            "news_status": 1

}


拉风的咖菲猫
浏览 189回答 2
2回答

交互式爱情

TextView content = mView.findViewById(R.id.content);String myHtml = "This will display an image to the right <img src='https://i-vnexpress.vnecdn.net/2019/06/06/trump-1571-1559784498.jpg' />";content.setText(Html.fromHtml(myHtml, Images, null));&nbsp; &nbsp; private Html.ImageGetter Images = new Html.ImageGetter() {&nbsp; &nbsp; &nbsp; &nbsp; public Drawable getDrawable(String source) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Drawable drawable = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FetchImageUrl fiu = new FetchImageUrl(getActivity(),source);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fiu.execute().get();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; drawable = fiu.GetImage();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; drawable = getResources().getDrawable(R.drawable.default_icon);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // to display image,center of screen&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(drawable!=null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int imgH = drawable.getIntrinsicHeight();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int imgW = drawable.getIntrinsicWidth();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int padding = 20;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int realWidth = 700; //ScreenW-(2*padding);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int realHeight = imgH * realWidth / imgW;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; drawable.setBounds(padding, 0, realWidth, realHeight);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return drawable;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };&nbsp; &nbsp; public class FetchImageUrl extends AsyncTask<String, String, Boolean> {&nbsp; &nbsp; &nbsp; &nbsp; String imageUrl;&nbsp; &nbsp; &nbsp; &nbsp; Context context;&nbsp; &nbsp; &nbsp; &nbsp; protected Drawable image;&nbsp; &nbsp; &nbsp; &nbsp; public FetchImageUrl(Context context, String url)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.imageUrl = url;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; image = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.context = context;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public Drawable GetImage()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return image;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; protected void onPreExecute() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super.onPreExecute();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; protected Boolean doInBackground(String... args) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InputStream input_stream = (InputStream) new URL(imageUrl).getContent();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; image = Drawable.createFromStream(input_stream, "src name");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; catch (Exception e)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; image = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; protected void onPostExecute(Boolean result) {&nbsp; &nbsp; &nbsp; &nbsp; }}

qq_花开花谢_0

像这样使用 -String htmlString = "<img src='ic_launcher'><i>Welcome to<i> <b><a href='http://android-coding.blogspot.com'>Android Coding</a></b>";&nbsp;@Override&nbsp;protected void onCreate(Bundle savedInstanceState) {&nbsp; super.onCreate(savedInstanceState);&nbsp; TextView htmlTextView = new TextView(this);&nbsp; setContentView(htmlTextView);&nbsp; htmlTextView.setText(Html.fromHtml(htmlString, new Html.ImageGetter(){&nbsp; &nbsp;@Override&nbsp; &nbsp;public Drawable getDrawable(String source) {&nbsp; &nbsp; Drawable drawable;&nbsp; &nbsp; int dourceId =&nbsp;&nbsp; &nbsp; &nbsp; getApplicationContext()&nbsp; &nbsp; &nbsp; .getResources()&nbsp; &nbsp; &nbsp; .getIdentifier(source, "drawable", getPackageName());&nbsp; &nbsp; drawable =&nbsp;&nbsp; &nbsp; &nbsp; getApplicationContext()&nbsp; &nbsp; &nbsp; .getResources()&nbsp; &nbsp; &nbsp; .getDrawable(dourceId);&nbsp; &nbsp; drawable.setBounds(&nbsp; &nbsp; &nbsp; 0,&nbsp;&nbsp; &nbsp; &nbsp; 0,&nbsp;&nbsp; &nbsp; &nbsp; drawable.getIntrinsicWidth(),&nbsp; &nbsp; &nbsp; drawable.getIntrinsicHeight());&nbsp; &nbsp; return drawable;&nbsp; &nbsp;}&nbsp; }, null));&nbsp; htmlTextView.setMovementMethod(LinkMovementMethod.getInstance());&nbsp;}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java