英文:
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): <img src="path" alt="">
What i have tried:
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);
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 = "<Put here the image>";
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>\n" +
"<body>\n" +
"<img id=\"image\" src=\""+imageUrl+"\" alt =\"no image\">\n" +
"</body></html>";
webView.loadDataWithBaseURL(null,html,"text/html", "UTF-8", null);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论