android 开发中 怎么用js获取手机屏幕高度

JavaScript010

android 开发中 怎么用js获取手机屏幕高度,第1张

webview.addjavascriptinterface可以调用android代码

android可以获得屏幕高度

DisplayMetrics dm = new DisplayMetrics()

getWindowManager().getDefaultDisplay().getMetrics(dm)

int height = dm.heightPixels//这个就是屏幕高度了。

webView.addJavascriptInterface(new WebAppInterface(this), "Android")

这个就创立了一个接口名,叫“Android”,运行在WebView中的JS代码可以通过这个名字调用WebAppInterface类中的showToast()方法:

<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />

<script type="text/javascript">

function showAndroidToast(toast)

{

Android.showToast(toast)

}

</script>

//获取盒子的内容高度,内容高度也可用用box.clientHeight获取,内容高度不包括边框和外边距和滚动条

var box = document.getElementById("box")

var contentHeight = window.getComputedStyle(box).height //输出 '60px'

//js获取移动端屏幕高度和宽度等设备尺寸,兼容性比较好的方法

document.documentElement.clientWidth

document.documentElement.clientHeight

此外,还可以使用元素的点击事件来获取元素高度等内容。