@JavascriptInterface

public void resize(final float height) {

WebViewActivity.this.runOnUiThread(new Runnable() {

@Override

public void run() {

//Toast.makeText(getActivity(), height + "", Toast.LENGTH_LONG).show();

RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)mWebView1.getLayoutParams();

layoutParams.width = getResources().getDisplayMetrics().widthPixels;

layoutParams.height = (int) (height * getResources().getDisplayMetrics().density);

mWebView.setLayoutParams(layoutParams);

}

});

}

or

@JavascriptInterface

public void resize(final float height) {

getActivity().runOnUiThread(new Runnable() {

@Override

public void run() {

//Toast.makeText(getActivity(), height + "", Toast.LENGTH_LONG).show();

mWebView.setLayoutParams(new LinearLayout.LayoutParams(getResources().getDisplayMetrics().widthPixels, (int) (height * getResources().getDisplayMetrics().density)));

}

});

}

mWebView.setWebViewClient(new WebViewClient() {

@Override

public void onPageFinished(WebView view, String url) {

mWebView.loadUrl("javascript:App.resize(document.body.getBoundingClientRect().height)");

super.onPageFinished(view, url);

}

});

mWebView.addJavascriptInterface(this, "App");

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐