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.
My problem is need to display some content i开发者_StackOverflow中文版n 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.
How i can achieve with the viewport meta tag or something else, to scale the page to fix automatically in the webiew size. (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. Then, back in HTML I am using JS methods to resize my document to be exactly the size of the available viewport.
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:
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). Your getScreenH() and getScreenW() must take into account the available viewport size, less any default window decorations (action bar, notifications bar etc.). It's a bit hacky, but works.
精彩评论