She*_*yar 66
是的,这是可能的.您可以尝试使用以下代码设置WebView布局.它会将所有图像(大于Device Screen Width)调整为屏幕宽度.这适用于两个方向(肖像和风景)
webview.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
Run Code Online (Sandbox Code Playgroud)
您可以稍后添加额外的边距/填充以使间距正确.
Yai*_*lka 38
webview.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
Run Code Online (Sandbox Code Playgroud)
有效,但已被弃用.这是没有LayoutAlgorithm.SINGLE_COLUMN的另一种解决方案,使用CSS:
@SuppressLint("NewApi")
private openWebView() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
webView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.TEXT_AUTOSIZING);
} else {
webView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.NORMAL);
}
String data = "<div> your HTML content </div>";
webView.loadDataWithBaseURL("file:///android_asset/", getHtmlData(data), "text/html", "utf-8", null);
}
private String getHtmlData(String bodyHTML) {
String head = "<head><style>img{max-width: 100%; width:auto; height: auto;}</style></head>";
return "<html>" + head + "<body>" + bodyHTML + "</body></html>";
}
Run Code Online (Sandbox Code Playgroud)
小智 15
您可以让浏览器调整图像大小以适合屏幕的最大宽度:
<img src="huge-image.jpg" width="100%" />
Run Code Online (Sandbox Code Playgroud)
也可以将其高度调整为WebView的视口:
<img src="huge-image.jpg" height="100%" />
Run Code Online (Sandbox Code Playgroud)
但是,调整宽度和高度都会导致图像拉伸.要根据哪一方最适合调整宽度或高度,您可以考虑使用一些JavaScript,如下所示:
<img src="huge-image.jpg" onload="resize(this);" />
<script type="text/javascript">
function resize(image)
{
var differenceHeight = document.body.clientHeight - image.clientHeight;
var differenceWidth = document.body.clientWidth - image.clientWidth;
if (differenceHeight < 0) differenceHeight = differenceHeight * -1;
if (differenceWidth < 0) differenceWidth = differenceWidth * -1;
if (differenceHeight > differenceWidth)
{
image.style['height'] = document.body.clientHeight + 'px';
}
else
{
image.style['width'] = document.body.clientWidth + 'px';
}
// Optional: remove margins or compensate for offset.
image.style['margin'] = 0;
document.body.style['margin'] = 0;
}
</script>
Run Code Online (Sandbox Code Playgroud)
And*_*dyB 15
你可以用这个:
WebView webView = (WebView) findViewById(R.id.myWebView);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
Run Code Online (Sandbox Code Playgroud)
在此处找到此解决方案: 如何设置webview的初始缩放/宽度
| 归档时间: |
|
| 查看次数: |
36550 次 |
| 最近记录: |