0

我加载a .jpg一个WebView. 我的问题是我发现了这个:Android WebView, Scaling Image to fit the screen

它对我不起作用。

这是我的代码:

Display display = getWindowManager().getDefaultDisplay();
    int width= display.getWidth();

    Toast.makeText(getApplicationContext(), ""+width, Toast.LENGTH_LONG).show();


    String html = "<html><head><title>Example</title><meta name=\"viewport\"\"content=\"width="+width+", initial-scale=0.65 \" /></head>";
    html+= "<body><img width=\""+width+"\"<img src=\""+"image.jpg"+"\" /></body></html>";

aboutText.loadDataWithBaseURL("file:///android_res/drawable/", html, "text/html","UTF-8" , null);
4

3 回答 3

5

这对我不起作用,因为我有高分辨率手机。

试试这个,它对我有用, webView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);

于 2013-12-03T05:22:28.947 回答
4

您可以在将 HTML 加载到 Web 视图之前设置 img 标记的样式。请看下面的代码

WebView content = (WebView) findViewById(R.id.webView1);
String head = "<head> <style>img{display: inline;height: auto;max-width:   100%;}</style> <style>body {font-family: 'Roboto';  }</style></head>";

content.loadDataWithBaseURL(null, head + post.getContent(), "text/html", "UTF-8", null);
于 2015-03-24T04:14:21.953 回答
2

你是 html 图片标签错误检查下面的代码:

Display display = getWindowManager().getDefaultDisplay();
int width= display.getWidth();

Toast.makeText(getApplicationContext(), ""+width, Toast.LENGTH_LONG).show();


String html = "<html><head><title>Example</title><meta name=\"viewport\"\"content=\"width="+width+", initial-scale=0.65 \" /></head>";
   html+= "<body><img width=\""+width+"\" src=\""+"image.jpg"+"\" /></body></html>";

aboutText.loadDataWithBaseURL("file:///android_res/drawable/", html, "text/html","UTF-8" , null);

您的 html 字符串格式错误,如下代码所示:

String html = "<html><head><title>Example</title><meta name=\"viewport\"\"content=\"width="+width+", initial-scale=0.65 \" /></head>";
html+= "<body><img width=\""+width+"\"<img src=\""+"image.jpg"+"\" /></body></html>";

格式化我的代码:

String html = "<html><head><title>Example</title><meta name=\"viewport\"\"content=\"width="+width+", initial-scale=0.65 \" /></head>";
html+= "<body><img width=\""+width+"\" src=\""+"image.jpg"+"\" /></body></html>";
于 2013-07-10T12:07:53.737 回答