1. 嵌套成功后webView高度为0不显示
2.需要在加载完成方法中重新设置webView高度
3.解决方法:
1)尝试
webView.getheight: 0不正确
webView.getContentHeight: 966 不正确
webView.getMeasureHeight: 0不正确
2)debug发现
webView.computeVerticalScrollRange()方法计算出来是真实内容高度
但此方法是protected权限,所以重写了一个自定义WebView拿到这个方法
4.示例
1)布局
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/nsd_view" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity2"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.example.laddingwu.adapterapplication.MeasuredWebView android:id="@+id/wb_content" android:layout_width="match_parent" android:layout_height="wrap_content" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点赞" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="收藏" /> </LinearLayout> </android.support.v4.widget.NestedScrollView>
2)自定义WebView
public class MeasuredWebView extends WebView { public MeasuredWebView(Context context) { super(context); } public MeasuredWebView(Context context, AttributeSet attrs) { super(context, attrs); } public MeasuredWebView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } /** * 计算webView内容的真实宽度 * @return */ public int getMeasureContentWidth() { return computeHorizontalScrollRange(); } /** * 计算webView内容的真实高度 * @return */ public int getMeasureContentHeight() { return computeVerticalScrollRange(); } }
3) 重新设置宽高
webView = findViewById(R.id.wb_content); initWebView(webView); webView.loadUrl("http://dzkc_about.deeptime.earth/dzkc.html");private void initWebView(final MeasuredWebView webView) { WebSettings webSettings = webView.getSettings(); webSettings.setDisplayZoomControls(false); webSettings.setJavaScriptEnabled(true); // 设置支持javascript脚本 webSettings.setAllowFileAccess(true); // 允许访问文件 webSettings.setBuiltInZoomControls(true); // 设置显示缩放按钮 webSettings.setSupportZoom(true); // 支持缩放 webSettings.setLoadWithOverviewMode(true); DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); int mDensity = metrics.densityDpi; if (mDensity == 240) { webSettings.setDefaultZoom(WebSettings.ZoomDensity.FAR); } else if (mDensity == 160) { webSettings.setDefaultZoom(WebSettings.ZoomDensity.MEDIUM); } else if (mDensity == 120) { webSettings.setDefaultZoom(WebSettings.ZoomDensity.CLOSE); } else if (mDensity == DisplayMetrics.DENSITY_XHIGH) { webSettings.setDefaultZoom(WebSettings.ZoomDensity.FAR); } else if (mDensity == DisplayMetrics.DENSITY_TV) { webSettings.setDefaultZoom(WebSettings.ZoomDensity.FAR); } else { webSettings.setDefaultZoom(WebSettings.ZoomDensity.MEDIUM); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING); } else { webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL); } webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS); webView.setWebViewClient(new WebViewClient(){ @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { webView.loadUrl(url); return true; } @Override public void onPageFinished(WebView view, String url) { int measuredHeight = webView.getMeasureContentHeight(); ViewGroup.LayoutParams params = webView.getLayoutParams(); params.width = getResources().getDisplayMetrics().widthPixels; params.height = measuredHeight; webView.setLayoutParams(params); super.onPageFinished(webView, url); } }); webView.setWebChromeClient(new WebChromeClient()); }