Html文本与Android中的Textview中的图像重叠

时间:2015-04-28 13:02:12

标签: android html textview

请不要将其标记为重复,因为其他帖子不能帮助我解决4.2以上的安卓

我正在实施一项任务,我必须将HTML内容显示在TextView内,除了图片外,一切正常。 某处文本与图像重叠我甚至在stackoverflow上也经历了很多帖子,但是没有取得任何成功。 我找到了来自This帖子的解决方案并获得了更好的图像,但是我仍然得到同样的关注。 作为参考,我也附加了一个屏幕截图。看看

我在某些设备上得到了这样的关注:Nexus-5,Galaxy-S3以及所有都有Android版本4.2 +

以下是我的代码:

public class URLImageParser implements ImageGetter 
{
    private Context oContext;
    private TextView container;
    private LayoutCustomization oLayoutCustomization;

/***
 * Construct the URLImageParser which will execute AsyncTask and refresh the container
 * @param oTextView
 * @param oContext
 */
public URLImageParser(TextView oTextView, Context oContext) 
{
    this.oContext = oContext;
    this.container = oTextView;
    oLayoutCustomization    = new LayoutCustomization(this.oContext.getResources().getDisplayMetrics());        
}

public Drawable getDrawable(String source) 
{
    URLDrawable urlDrawable = new URLDrawable();

    // get the actual source
    ImageGetterAsyncTask asyncTask = 
        new ImageGetterAsyncTask( urlDrawable);

    asyncTask.execute(source);

    // return reference to URLDrawable where I will change with actual image from
    // the src tag
    return urlDrawable;
}

public class ImageGetterAsyncTask extends AsyncTask<String, Void, Drawable>  
{
    URLDrawable urlDrawable;

    public ImageGetterAsyncTask(URLDrawable d) 
    {
        this.urlDrawable = d;
    }

    @Override
    protected Drawable doInBackground(String... params) 
    {
        String source = params[0];
        return fetchDrawable(source);
    }

    @Override
    protected void onPostExecute(Drawable result) 
    {
        if(result == null)
        {
            urlDrawable.drawable = oContext.getResources().getDrawable(R.drawable.ic_launcher);

            // redraw the image by invalidating the container
            URLImageParser.this.container.invalidate();
            return;
        }
        // set the correct bound according to the result from HTTP call
        urlDrawable.setBounds(0, 0, 0 + result.getIntrinsicWidth(), 
                0 + result.getIntrinsicHeight()); 

        // change the reference of the current drawable to the result
        // from the HTTP call
        urlDrawable.drawable = result;

        // redraw the image by invalidating the container
        URLImageParser.this.container.invalidate();

     // For ICS
        URLImageParser.this.container.setHeight((URLImageParser.this.container.getHeight() + result.getIntrinsicHeight()));

        // Pre ICS
        URLImageParser.this.container.setEllipsize(null);

        URLImageParser.this.container.setText(URLImageParser.this.container.getText());
    }

    /***
     * Get the Drawable from URL
     * @param urlString
     * @return
     */
    public Drawable fetchDrawable(String urlString) 
    {
        try 
        {
            URL aURL = new URL(urlString);
            final URLConnection conn = aURL.openConnection(); 
            conn.connect(); 
            final BufferedInputStream bis = new BufferedInputStream(conn.getInputStream()); 
            final Bitmap bm = BitmapFactory.decodeStream(bis);
            @SuppressWarnings("deprecation")
            Drawable drawable = new BitmapDrawable(bm);
            drawable.setBounds(0,0,bm.getWidth(),bm.getHeight());
            return drawable;
        } 
        catch (Exception e) 
        {
            return null;
        } 
    }
}`

并且

public class URLDrawable extends BitmapDrawable 
{
    // the drawable that you need to set, you could set the initial drawing
    // with the loading image if you need to
    protected Drawable drawable;

    @Override
    public void draw(Canvas canvas) 
    {
        // override the draw to facilitate refresh function later
        if(drawable != null) 
        {
            drawable.draw(canvas);
        }
    }
}

这就是我在上面的代码中实现的方式:

URLImageParser oImageParser = new URLImageParser(oTextView, oContext);
Spanned htmlSpan = Html.fromHtml("SOME_HTML_STRING", null);
oTextView.setText(htmlSpan);

请帮忙。

2 个答案:

答案 0 :(得分:1)

onPostExecute

中重新设置文字

实施例。 txtTest.setText(txtTest.getText());

答案 1 :(得分:0)

更改以下代码

Spanned htmlSpan = Html.fromHtml("SOME_HTML_STRING", null); 

Spanned htmlSpan = Html.fromHtml("SOME_HTML_STRING", oImageParser);