在Android WebView上适配图像

huangapple go评论142阅读模式
英文:

Fitting Image on Android WebView

问题

我正在使用 WebView,并希望在其中显示一张图片。

我想要将图片适应 WebView。(图片变得很大,因此我必须在水平方向上滚动)

我像这样显示图片(如果有影响的话):<img src="路径" alt="">

我尝试过的方法:

WebView content = (WebView) findViewById(R.id.webView1);
content.loadDataWithBaseURL(null, "<style>img{display: inline;height: auto;max-width: 100%;}</style>" + CONTENT, "text/html", "UTF-8", null);

但是当我使用这种方法时,图片是不可见的,只有文本。

我还尝试了这个:

WebSettings settings = preview.getSettings();
settings.setLoadWithOverviewMode(true);
settings.setBuiltInZoomControls(true);

这个方法可以工作,但效果不好,因为它会缩小 WebView,文本变得难以阅读。
有其他的方法吗?

英文:

I'm using WebView and i want to display an image inside it.

I want to fit the image in webview. (The image becomes so big so i have to scroll horizontally)

I show images like this (if it matters): &lt;img src=&quot;path&quot; alt=&quot;&quot;&gt;

What i have tried:

WebView content = (WebView) findViewById(R.id.webView1);
content.loadDataWithBaseURL(null, &quot;&lt;style&gt;img{display: inline;height: auto;max-width: 100%;} 
&lt;/style&gt;&quot; + CONTENT, &quot;text/html&quot;, &quot;UTF-8&quot;, null);

But when i use this method, the images are not visible. Only texts

Also i tried this:

   WebSettings settings = preview.getSettings();
   settings.setLoadWithOverviewMode(true);
   settings.setBuiltInZoomControls(true);

It works but it's not good since it zoom out the webview and the texts become unreadable.
Is there any other way?

答案1

得分: 1

以下是翻译好的代码部分:

String imageUrl = "<在这里放入图片链接>";
String html = "<html><head>" +
        "<meta name=\"viewport\" content=\"width=device-width, user-scalable=no\">" +
        "<style>\n" +
        "html,body{ margin:auto;}" +
        "#image{\n" +
        "    width: 100vw;\n" +
        "    height: 100vh;\n" +
        "}\n" +
        "</style></head>" +
        "<body>" +
        "<img id=\"image\" src=\"" + imageUrl + "\" alt=\"无图像\">" +
        "</body></html>";
webView.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null);
英文:

if you use this code it will allow you to fit all the image in the webview

String imageUrl = &quot;&lt;Put here the image&gt;&quot;;
		String html = &quot;&lt;html&gt;&lt;head&gt;&quot; +
				&quot;&lt;meta name=\&quot;viewport\&quot; content=\&quot;width=device-width, user-scalable=no\&quot;&gt;&quot;+
				&quot;&lt;style&gt;\n&quot; +
				&quot;html,body{ margin:auto;}&quot;+
				&quot;#image{\n&quot; +
				&quot;    width: 100vw;\n&quot; +
				&quot;    height: 100vh;\n&quot; +
				&quot;}\n&quot; +
				&quot;&lt;/style&gt;&lt;/head&gt;\n&quot; +
				&quot;&lt;body&gt;\n&quot; +
				&quot;&lt;img id=\&quot;image\&quot; src=\&quot;&quot;+imageUrl+&quot;\&quot; alt =\&quot;no image\&quot;&gt;\n&quot; +
				&quot;&lt;/body&gt;&lt;/html&gt;&quot;;
		webView.loadDataWithBaseURL(null,html,&quot;text/html&quot;, &quot;UTF-8&quot;, null);

huangapple
  • 本文由 发表于 2020年5月5日 01:21:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/61597996.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定