【问题标题】:Html Text is overlapping with image in Textview in AndroidHtml文本与Android Textview中的图像重叠
【发布时间】:2015-07-07 08:33:39
【问题描述】:

请不要将其标记为重复,因为其他帖子不要帮助我为 4.2 以上的 android 提供帮助

我正在执行一项任务,其中我必须将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);

请帮忙。

【问题讨论】:

  • @N5: 创建动态TextView
  • 发布您的完整代码

标签: android html textview


【解决方案1】:

修改下面的代码

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

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

【讨论】:

    【解决方案2】:

    onPostExecute中重新设置文字。

    例如。 txtTest.setText(txtTest.getText());

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-13
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 2016-09-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多