如何使newWebView缩放以适合不同设备的屏幕?
因此,我使用表创建了一些简单的HTML页面,然后将其保存到文件中(带有下面的meta标签)。我发现了meta标签,但即使是下面的标签,它也可以在iPhone 5上很好地工作,而在iPad上,它仅占用屏幕宽度的1/2?
<head>
<meta name = "viewport" content = "width = device-width">
</head>
不知道这是否相关,但是我的config.lua具有:
local aspectRatio = display.pixelHeight / display.pixelWidth
local width = aspectRatio > 1.5 and 320 or math.ceil( 480 / aspectRatio )
local height = aspectRatio < 1.5 and 480 or math.ceil( 320 * aspectRatio )
application = {
content = {
width = width,
height = height,
scale = "letterBox",
fps = 60,
imageSuffix = {
["@2x"] = 1.5,
["@3x"] = 3.0,
},
},
}
编辑:设置代码:
local displayW, displayH = display.contentWidth, display.contentHeight
local webViewHeight = displayH - navBar.height
webView = native.newWebView(
displayW/2, navBar.height + webViewHeight/2,
displayW, webViewHeight
)
scene.view:insert(webView)
我还在您的config.lua中使用了相同的代码,为了使其适合这里,这是我用于Webview的内容:
local _H = display.contentHeight
local _W = display.contentWidth
local SBH = display.topStatusBarContentHeight -- Status bar height
webView = native.newWebView( 0, SBH, _W, _H-SBH )
webView.anchorX = 0
webView.anchorY = 0
webView:request( "file.html", system.DocumentDirectory ) -- make sure you choose the right location of your file
API:
native.newWebView( centerX, centerY, width, height )
如您从我的代码中看到的那样,我已经将锚点设置为TopLeft,因为这样可以很容易地将其定位。然后,width占据整个屏幕,高度为contentHeight减去状态栏的高度。
此解决方案可在所有设备(iOS和Android)上运行。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句