简体   繁体   English

Android Webview视口元数据(需要适合Webview的内容)

[英]Android webview viewport meta data (need content fit to webview)

I read all possible questions/response on stackoverflow, but i still can't do what i would like. 我阅读了关于stackoverflow的所有可能的问题/响应,但是我仍然无法做我想做的事情。

My problem is need to display some content in webview (from asset folder, so i can edit the html), and i need to set the webview size, lets say 200dip x 400dip, or any other size. 我的问题是需要在webview中显示一些内容(从资产文件夹,以便我可以编辑html),并且我需要设置webview的大小,例如200dip x 400dip或任何其他大小。

How i can achieve with the viewport meta tag or something else, to scale the page to fix automatically in the webiew size. 如何使用视口meta标签或其他方法实现缩放页面以自动固定在webiew大小中。 (No scroll, etc). (无滚动等)。

Somebody have any idea? 有人有主意吗?

The way I have solved this is by using a callback from JS back to my Android JS handler, asking for the webview dimensions. 我解决此问题的方法是使用从JS返回我的Android JS处理程序的回调,要求提供Webview尺寸。 Then, back in HTML I am using JS methods to resize my document to be exactly the size of the available viewport. 然后,回到HTML中,我使用JS方法将文档的大小调整为与可用视口相同的大小。

Something along these lines: 遵循以下原则:

webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(this, "android");
webView.loadUrl("file:///android_asset/start.html");

Then, in start.html, in the onLoad() event handler I have: 然后,在start.html中,在onLoad()事件处理程序中,我具有:

h = android.getScreenH();
w = android.getScreenW();

Then I set my viewport properties via CSS (I'm using jQuery in my HTML, so it's much easier to do). 然后,我通过CSS设置视口属性(我在HTML中使用jQuery,因此更容易实现)。 Your getScreenH() and getScreenW() must take into account the available viewport size, less any default window decorations (action bar, notifications bar etc.). 您的getScreenH()和getScreenW()必须考虑可用的视口大小,减去任何默认的窗口装饰(动作栏,通知栏等)。 It's a bit hacky, but works. 这有点hacky,但可以。

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

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