简体   繁体   English

WebView中的视口元标记无法在Android中正确缩放内容

[英]Viewport metatag in WebView not scaling the contents properly in Android

I have a WebView inside a ScrollView. 我在ScrollView中有一个WebView。 This is my first requirement as there will be other elements also inside the ScrollView. 这是我的第一个要求,因为ScrollView中还将包含其他元素。 I want the WebView to fit the content in the device-width available. 我希望WebView使内容适合可用的设备宽度。 For this I am using viewport meta tag as shown below. 为此,我使用视口元标记,如下所示。

<meta name=\"viewport\" content=\"target-densitydpi=device-dpi, initial-scale=1, width=device-width\" />

WebView's height is set to Wrap_Content. WebView的高度设置为Wrap_Content。 I am loading the WebView on button click using loadDataWithBaseURL() method. 我正在使用loadDataWithBaseURL()方法在单击按钮时加载WebView。 Now the problem is that it is causing the content below the WebView flicker and the WebView content is not sizing properly. 现在的问题是,它导致WebView下方的内容闪烁,并且WebView内容的大小不正确。 It sometimes leave the space below the WebView content. 有时它会在WebView内容下方留出空间。 When I remove the metag tag, WebView content loads with desired size but doesn't fit in the viewport. 当我删除metag标记时,WebView内容将按所需大小加载,但不适合视口。

Any suggestions? 有什么建议么?

This is probably caused by the fact that the WebView processes the viewport meta-tag asynchronously. 这可能是由于WebView异步处理视口元标记而引起的。 This means that for a brief moment the WebView uses a default viewport (320 px wide) and if your content is sufficiently complex it might be possible for the first layout pass to occur while the default viewport is still in effect. 这意味着,WebView会在短时间内使用默认视口(320 px宽),如果您的内容足够复杂,则可能会在默认视口仍然有效的情况下进行第一次布局。

I've never tried this but you could make the content of the page not display (something like style="display: none") until the viewport tag has been processed (window.onload should be sufficient). 我从没有尝试过,但是您可以使页面的内容不显示(类似于style =“ display:none”),直到处理完视口标记(window.onload应该足够)为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM