【问题标题】:Fitting Image on Android WebView在 Android WebView 上拟合图像
【发布时间】:2020-08-19 05:16:27
【问题描述】:

我正在使用 WebView,我想在其中显示一个图像。

我想在 webview 中调整图像。 (图像变得如此之大,我不得不水平滚动)

我会展示这样的图片(如果重要的话):<img src="path" 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);

它可以工作,但它并不好,因为它缩小了 web 视图并且文本变得不可读。 有没有其他办法?

【问题讨论】:

    标签: java android html image webview


    【解决方案1】:

    如果您使用此代码,它将允许您将所有图像放入 web 视图中

    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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-28
      相关资源
      最近更新 更多