android - 在 TextView 中显示来自 HTML 标记的图像?

标签 android html image

XML 布局

<org.sufficientlysecure.htmltextview.HtmlTextView
            android:id="@+id/txtDescription"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:lineSpacingExtra="5dp"
            android:padding="5dp"
            android:textAppearance="@style/TextAppearance.AppCompat.Small"
            android:textColor="@color/black" />

java

txtDescription.setHtml(object.getString("content"), new HtmlHttpImageGetter(txtDescription));

服务器响应

{"success":"true","Content":{"id":115,"title":"vvipprogram","content":"<img src=\"https://example.com//media//images//topvvip.jpg" alt=\"topvvip\" width=\"43%\"\/>\r\n"}}

结果截图

enter image description here

我想在整页中显示这张图片吗?这该怎么做 ?请帮助我。

最佳答案

您可以使用此代码从 textview 设置 html 图像和文本:

这是 xml 代码:

      <TextView
        android:id="@+id/txtDescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:lineSpacingExtra="5dp"
        android:padding="5dp"
        android:textAppearance="@style/TextAppearance.AppCompat.Small"
        android:textColor="@color/black" />

这是 Java 代码:

public class ConditionOfUseActivity extends Activity implements Html.ImageGetter {
     private TextView txtDescription;
     private Drawable empty;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_condition_use);
        txtDescription = (TextView) findViewById(R.id.txtDescription);
        String source = "this is a test of <b>ImageGetter</b> it contains " +
            "two images: <br/>" +
            "<img src=\"http://developer.android.com/assets/images/dac_logo.png\"><br/>and<br/>" +
            "<img src=\"http://developer.android.com/assets/images/icon_search.png\">";           
        Spanned spanned = Html.fromHtml(object.getString("content"), ConditionOfUseActivity.this, null);
        txtDescription.setText(spanned); 
     }

     @Override
     public Drawable getDrawable(String s) {
       LevelListDrawable d = new LevelListDrawable();
       empty = getResources().getDrawable(R.drawable.splash1);
       d.addLevel(0, 0, empty);
       d.setBounds(0, 0, empty.getIntrinsicWidth(), empty.getIntrinsicHeight());
       new LoadImage().execute(s, d);
       return d;
    }

    class LoadImage extends AsyncTask<Object, Void, Bitmap> {
       private LevelListDrawable mDrawable;

       @Override
       protected Bitmap doInBackground(Object... params) {
          String source = (String) params[0];
          mDrawable = (LevelListDrawable) params[1];
          Log.d(TAG, "doInBackground " + source);
          try {
            InputStream is = new URL(source).openStream();
            return BitmapFactory.decodeStream(is);
          } catch (FileNotFoundException e) {
            e.printStackTrace();
          } catch (MalformedURLException e) {
            e.printStackTrace();
          } catch (IOException e) {
            e.printStackTrace();
          }
        return null;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (bitmap != null) {
            BitmapDrawable d = new BitmapDrawable(bitmap);
            mDrawable.addLevel(1, 1, d);
            //mDrawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
            mDrawable.setBounds(0, 0, empty.getIntrinsicWidth(), empty.getIntrinsicHeight());
            mDrawable.setLevel(1);
            CharSequence t = txtDescription.getText();
            txtDescription.setText(t);
        }
    }
}

关于android - 在 TextView 中显示来自 HTML 标记的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46913784/

相关文章:

android - Android Gradle 插件 0.13 和 android-apt 1.3 出错

android - 在没有 MediaPlayer 的情况下使用 RemoteControlClient

android - 添加 Xam.Plugin.Connectivity GooglePlayconsole 后的 Xamarin Forms 对签名 APK 用户功能发出警告

html - 加载 HTML 文件时浏览器跳转到页面中间

javascript - Jquery slider 向下移动如何使其水平移动

php - 签名 png 不显示

java - 查看寻呼机 - 使用静态变量以编程方式滑动到下一页

html - Twitter Bootstrap - 内联表单

image - css3旋转图像保留其左上角位置

javascript - jQuery 图像幻灯片无法正常工作