art*_*ros 44 android android-webview
我有:我正在从URL加载图像.我只是这样做(WebView).loadUrl(imageurl, extraheaders)
我得到的:图像没有在WebView的全宽处显示,它周围有空白区域(就像你在桌面浏览器中打开一点点)
我尝试了什么:将LayoutAlgorithm设置为SINGLE_COLUMN.工作完美,但缩放不起作用.(我已经启用它了不WebView.getSettings()知道为什么.设置setUseWideViewPort(true);加载图像没有任何空白空间,但它完全放大.
Ton*_*ony 85
你也可以这样做.
img在数据字符串的开头添加CSS样式(取决于您的Web数据格式).
<style>img{display: inline; height: auto; max-width: 100%;}</style>
Run Code Online (Sandbox Code Playgroud)
为了在WebView中快速完成数据,我做到了这一点.
WebView content = (WebView) findViewById(R.id.webView1);
content.loadDataWithBaseURL(null, "<style>img{display: inline;height: auto;max-width: 100%;}</style>" + post.getContent(), "text/html", "UTF-8", null);
Run Code Online (Sandbox Code Playgroud)
它与bansal21ankit所说的非常相似,但它可以在HTML中的每个图像上运行而无需额外的工作.
编辑(关于帖子内容的说明):
您可以使用任何text/html值而不是post.getContent()示例.
这里的帖子内容只是一个内容的示例,该text/html内容从某些数据源加载,然后与style使给定内容中的任何图像适合屏幕的部分连接.
Seb*_*eit 47
我有同样的问题,这样做很好:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
String data = "<html><head><title>Example</title><meta name=\"viewport\"\"content=\"width="+width+", initial-scale=0.65 \" /></head>";
data = data + "<body><center><img width=\""+width+"\" src=\""+url+"\" /></center></body></html>";
webView.loadData(data, "text/html", null);
Run Code Online (Sandbox Code Playgroud)
编辑:由于这仍然是公认的答案,这里有一个更好的解决方案(以下所有归功于Tony):
WebView content = (WebView) findViewById(R.id.webView1);
content.loadDataWithBaseURL(null, "<style>img{display: inline;height: auto;max-width: 100%;}</style>" + post.getContent(), "text/html", "UTF-8", null);
Run Code Online (Sandbox Code Playgroud)
the*_*osh 39
你应该缩放webView以适应屏幕:
WebView data = (WebView) getViewById(R.id.webview1);
data.getSettings().setLoadWithOverviewMode(true);
data.getSettings().setUseWideViewPort(true);
Run Code Online (Sandbox Code Playgroud)
对我有用的是:我读到为了适应屏幕的宽度,你应该将它添加到字段内的HTML或xml中:
width="100%"
Run Code Online (Sandbox Code Playgroud)
所以我做的是,而不是缩放和缩放图像,我得到了xml,把它放在一个StringBuilder中,找到了src ="https://blablabla.com/image.png",它位于字段内部,就在之前"src"子字符串我插入了"width ="100%"",然后用StringBuilder设置我的webView,mi代码是这样的:
public void setWebViewWithImageFit(String content){
// content is the content of the HTML or XML.
String stringToAdd = "width=\"100%\" ";
// Create a StringBuilder to insert string in the middle of content.
StringBuilder sb = new StringBuilder(content);
int i = 0;
int cont = 0;
// Check for the "src" substring, if it exists, take the index where
// it appears and insert the stringToAdd there, then increment a counter
// because the string gets altered and you should sum the length of the inserted substring
while(i != -1){
i = content.indexOf("src", i + 1);
if(i != -1) sb.insert(i + (cont * stringToAdd.length()), stringToAdd );
++cont;
}
// Set the webView with the StringBuilder: sb.toString()
WebView detailWebView = (WebView) findViewById(R.id.web_view);
detailWebView.loadDataWithBaseURL(null, sb.toString(), "text/html", "utf-8", null);
}
Run Code Online (Sandbox Code Playgroud)
希望这会有所帮助,我花了几个小时才弄明白如何解决这个问题.
它有点晚了,但我希望这会有所帮助,你能做的是:
String html = "<html><body><img src=\"" + URL + "\" width=\"100%\" height=\"100%\"\"/></body></html>";
mWebView.loadData(html, "text/html", null);
Run Code Online (Sandbox Code Playgroud)
这将使图像与WebView的宽度完全相似,其余的可以调整WebView本身.
小智 6
代码:
web = (WebView) findViewById(R.id.at_image_web);
web.getSettings().setBuiltInZoomControls(true);
web.getSettings().setUseWideViewPort(true);
web.getSettings().setJavaScriptEnabled(true);
web.getSettings().setLoadWithOverviewMode(true);
web.loadUrl(linkImage);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
59949 次 |
| 最近记录: |