[英]TextView with HTML content - Hide images/empty squares
我有html內容並將其設置為(android)文本框,如下所示
textbox.setText(Html.fromHtml(myhtml));
HTML內容也包含一些img標簽,我不希望顯示圖像,當前它在有img標簽的地方顯示空的方框。 如何隱藏那些方形框?
您可以在htmlString.
上用正則表達式替換<img.+?>
htmlString.
textView.setText(Html.fromHtml(htmlString.replaceAll("<img.+?>", "")));
未經測試
更新:
由於圖像看起來像:
<img ...></img>
和
<img... />
此解決方案將同時滿足以下兩種情況:
String htmlBody = htmlString.replaceAll("<img.+/(img)*>", "");
textView.setText(Html.fromHtml(htmlBody));
這有效
創建Html.ImageGetter以返回空圖像,如下所示
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.text.Html;
public class EmptyImageGetter implements Html.ImageGetter {
private static final Drawable TRANSPARENT_DRAWABLE = new ColorDrawable(Color.TRANSPARENT);
@Override
public Drawable getDrawable(String source) {
return TRANSPARENT_DRAWABLE;
}
}
然后創建一個靜態實例
private static final EmptyImageGetter EMPTY_IMAGE_GETTER = new EmptyImageGetter();
將此設置為文本視圖
textView.setText(Html.fromHtml(myhtml, EMPTY_IMAGE_GETTER, null));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.