如何在Corona SDK中让newWebView缩放适应不同设备的屏幕?

如何使得 newWebView 缩放适应不同设备的屏幕?

我正在创建一些简单的 HTML 页面和表格,并将其保存到文件中(使用以下 meta 标记)。我发现了这个 meta 标记,但即使使用以下代码,在 iPhone 5 上效果很好,在 iPad 上只占屏幕宽度的一半?

 <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)
点赞
用户841531
用户841531

我还在你的config.lua中使用了相同的代码,为了使它适用于这里,以下是我在网页视图中使用的代码:

local _H  = display.contentHeight
local _W  = display.contentWidth
local SBH = display.topStatusBarContentHeight -- 状态栏高度
webView   = native.newWebView( 0, SBH, _W, _H-SBH )
webView.anchorX = 0
webView.anchorY = 0
webView:request( "file.html", system.DocumentDirectory ) -- 确保你选择了正确的文件位置

API:

native.newWebView( centerX, centerY, width, height )

如你从我的代码中可以看到的一样,我将锚点设置为左上角,因为这样很容易定位。然后,宽度占据整个屏幕,高度是内容高度减去状态栏高度。

这个解决方案适用于所有设备,包括iOS和Android。

2014-03-12 14:58:49