2013-07-10 154 views
0

我在WebView中加载a .jpg。我的问题是,我发现这个:Android WebView, Scaling Image to fit the screenAndroid中的Webview图像适合屏幕

它不适合我。

这里是我的代码:

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

你试过我的回答吗? –

+0

这是正确的解决方案:http://stackoverflow.com/a/23656581/550393 – 2cupsOfTech

回答

1

你是下面的代码的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>"; 
5

这不适合我,因为我有高分辨率手机。

试试这个,它的工作对我来说, webView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);

+0

它适用于我的情况,thx。 – derjohng

+0

嗡嗡声,它适用于我的GenyMotion模拟器,但不适用于我的Xperia Z ... – Cocorico

4

您可以风格img标签加载HTML网页到视图之前。请参阅下面的代码

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

这似乎工作得很好! – amp