添加数据后如何调整android webview的大小 [英] How to resize a android webview after adding data in it

查看:21
本文介绍了添加数据后如何调整android webview的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在布局(线性、垂直)层次结构中,我有几个视图,其中一个是 WebView.它们都有相同的参数:

In a layout (linear, vertical) hierarchy I've got several views and one of them is a WebView. They all have same parameters:

android:layout_width="fill_parent"
android:layout_height="wrap_content"

对于所有视图,元素之间的间距都得到了正确处理,但对于 Webview,内部显示的文本后面有很多空格.

For all views, the spacing between elements is correctly handled but for the Webview there is a lot of spaces after the displayed text inside.

我使用布局在活动的onCreate"方法中设置了 webview 的 (HTML) 文本.

I set the (HTML) text of the webview in the "onCreate" method of the activity using the layout.

有没有办法让 webview 调整自己的大小,以匹配其内容大小?

Is there a way to let the webview resize itself, to match its content size?

顺便说一下,不同设备的剩余空间不同(模拟器 vs Milestone vs Hero)

By the way, the space left vary from one device to another (emulator vs Milestone vs Hero)

有什么想法吗?谢谢

推荐答案

我确信这为时已晚,但添加对我有用的方法,以防其他人来这里寻找解决方案.

I am sure this is too late but adding the method which worked for me in case somebody else comes here looking for solution.

页面加载完成后,我将注入一个 javascript 方法来回调我的 JS 钩子.在这种方法中,我传递了 .

Once the page finishes loading, I am injecting a javascript method to callback me JS hook. And in this method I am passing the size of .

    private void setupWebView() {
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            webView.loadUrl("javascript:MyApp.resize(document.body.getBoundingClientRect().height)");
            super.onPageFinished(view, url);
        }
    });
    webView.addJavascriptInterface(this, "MyApp");
}
@JavascriptInterface
public void resize(final float height) {
    MyActivity.this.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            webView.setLayoutParams(new LinearLayout.LayoutParams(getResources().getDisplayMetrics().widthPixels, (int) (height * getResources().getDisplayMetrics().density)));
        }
    });
}

同样的解决方案也可以看到这里.

The same solution can also be seen here.

这篇关于添加数据后如何调整android webview的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆