我的HTML文本中间的图像

时间:2017-10-26 14:02:58

标签: android html xamarin xamarin.android

我得到一个带有命令html文本和图像的xml。我想在获取标签时img在文本中生成图像。在IOS中正常工作

namespace A.iOS.Renderer
{
public class CustomLabelRenderer : LabelRenderer
{
    protected LineSpacingLabel LineSpacingLabel { get; private set; }

    protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
    {
        base.OnElementChanged(e);
        var attr = new NSAttributedStringDocumentAttributes();
        var nsError = new NSError();
        attr.DocumentType = NSDocumentType.HTML;
        Control.AdjustsFontSizeToFitWidth = true;
        var myHtmlData = NSData.FromString(Control.Text, NSStringEncoding.Unicode);
        this.Control.AttributedText = new NSAttributedString(myHtmlData, attr, ref nsError);
        this.Control.Font = UIFont.FromName("Lato", 20f);
        Control.LineBreakMode = UILineBreakMode.Clip;
    }
  }
 }

在android中他甚至可以识别图像,但只是向我展示了一个没有图像的小方块

namespace A.Droid.Renderer
{
   public class CustomLabelRenderer : LabelRenderer
   {
    protected LineSpacingLabel LineSpacingLabel { get; private set; }

    protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
    {
        base.OnElementChanged(e);
        var label = (TextView)Control;

        label.SetLineSpacing(11, 1);
        label.TextFormatted = Html.FromHtml(Control.Text);
        Control.MovementMethod = LinkMovementMethod.Instance; // Makes links clickable
        this.UpdateLayout();
    }
}

}

我必须在android中的命令中更改,以便我的图像显示在标签上?

1 个答案:

答案 0 :(得分:0)

我希望它适合你。

的onCreate()

Spanned spannedValue = Html.fromHtml("Your Html", getImageHTML(), null);
YourTextView.setText(spannedValue);

功能:

public Html.ImageGetter getImageHTML() {
    Html.ImageGetter imageGetter = new Html.ImageGetter() {
        public Drawable getDrawable(String source) {
            DownloadImageTask task = new DownloadImageTask();
            try {
                return task.execute(new String[] { source }).get();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
            return null;
        }
    };
    return imageGetter;
}

private class DownloadImageTask extends AsyncTask<String, Void, Drawable> {
    protected Drawable doInBackground(String... urls) {
        for(String s : urls) {
            try {
                Drawable drawable = Drawable.createFromStream(new URL(s).openStream(), "src name");
                Display display = getWindowManager().getDefaultDisplay();
                Point size = new Point();
                display.getSize(size);
                Integer width = size.x;
                Integer height = size.y;
                Integer heigtbol = height / 3;
                drawable.setBounds(0, 0, width, heigtbol);

                return drawable;
            } catch (IOException exception) {
                Log.v("IOException", exception.getMessage());
                return null;
            }
        }
        return null;
    }

    @Override
    protected void onPostExecute(Drawable drawable) {
        super.onPostExecute(drawable);
    }
}