当ScrollView嵌套WebView的时候遇到的问题

当ScrollView嵌套WebView的时候,如果需要调用WebView的setTextSise的时候,会出现底部一大片空白,试了很多方法都不管用,无意中找到了一个解决方案,居然可以了,我也是醉了,当初居然没有想到这么做.好了,下面看代码吧。
首先写一个重置WebView高度的方法
@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)));
    }
});

}
复制代码
然后在你的webview中注入JS,动态获取这个html内容的高度。
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");

}
复制代码
当每一次调用setTextSize的时候只需要
webView.loadUrl("javascript:MyApp.resize(document.body.getBoundingClientRect().height)");
复制代码
重新调用这句话就OK了。

已有 11 条评论
  1. Austot

    Bentyl Generic Levitra 40 Mg No Prescription cheapest cialis Avelox Cost Of Viagra At Walmart Can Propecia Stop Receding Hairline

    Austot 回复
  2. Austot

    Kamagra Oral Jelly Acheter No Rx Prednisone Free Amoxicillin buy fincar on line Propecia Receding Hair Growth

    Austot 回复
  3. LesSity

    Cialis Funziona Depressione viagra Thyrox 200 Without A Prescription Dianiben 850 No Prescription

    LesSity 回复
  4. Ellcurfak

    Best Sites For Low Price Cialis Levitra Comparison viagra Find Clobetasol 30g Price Of Levitra In Canada

    Ellcurfak 回复
  5. Ellcurfak

    Treatment For Allergic Reaction To Amoxicillin Foro Compra Viagra cheap levitra online Viagra Kaufen Amerika Cialis Con Cerveza Isotretinoin amnesteem skin health medicine on line

    Ellcurfak 回复
  6. RebPlesum

    Acheter Du Viagra En Suisse generic cialis overnight delivery Levitra Horschaden

    RebPlesum 回复
  7. Ellcurfak

    Does Amoxicillin Affect Apri Cephalexin Sids Affects precio de cialis generico Levitra Is Ordering Isotretinoin

    Ellcurfak 回复
  8. Austot

    Levitra Generico Online Italia Fast Shipping Sildenafil Citrate Where To Purchase Bentyl para que sirve levitra 20 mg Zithromax 200 Mg Commander Cialis Pas Cher

    Austot 回复
  9. Austot

    Acquisto Cialis Italia Prix Pilule Viagra En Pharmacie viagra Viagra Pfizer Apotheke Soft Viagra Cpt Injection Cephalexin

    Austot 回复
  10. Ellcurfak

    Viagra Plus Cialis20 Mg Cialis Propecia Soluzioni cialis vs viagra Ventolin Canada Zithromax To Treat Eczema Mupirocin Cream 5 Mail Order Canada

    Ellcurfak 回复
  11. LesSity

    Doxycycline 10mg Finasteride Vs Propecia Viagra Wonderful Special For Men cialis online Cialis 10 Effet Cialis Garantito Tiempo Duracion Priligy

    LesSity 回复
发表新评论