scrollView 中嵌套webview 我遇到两个问题:
1,滑动冲突 scrollview和webview都有滑动功能,可是他们会冲突啊
2,webview在 不能显示指定url网页数据,实际上是加载了的,其他手机正常显示,华为荣耀8等部分手机中显示空白,究其原因是高的问题,若设置固定高则可以显示数据,但是这是不合理的办法
解决办法:采用重写webView的方法进行解决,代码如下:
public class NoScrollWebView extends WebView { @SuppressLint("NewApi") public NoScrollWebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } public NoScrollWebView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public NoScrollWebView(Context context, AttributeSet attrs) { super(context, attrs); } public NoScrollWebView(Context context) { super(context); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int mExpandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, mExpandSpec); } }
xml中使用方法:
<ScrollView android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.freedev.tool.view.NoScrollWebView android:id="@+id/webViewNet" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="15dp" /> </LinearLayout> </ScrollView>
activity中使用:
if (webView != null) { webView.loadUrl(res.getData().getContent()); }
这里需要注意的是webview的初始化,这里贴出代码如下:
@SuppressLint("SetJavaScriptEnabled")
private void initWebview() {
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
//启用应用缓存
webSettings.setAppCacheEnabled(false);
webSettings.setDatabaseEnabled(false);
//开启DOM缓存,关闭的话H5自身的一些操作是无效的
webSettings.setDomStorageEnabled(true);
//适应屏幕
webSettings.setLoadWithOverviewMode(true);
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
//解决android7.0以后版本加载异常问题
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
view.loadUrl(request.getUrl().toString());
} else {
view.loadUrl(request.toString());
}
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Logger.d("----------onPageFinished ");
}
@TargetApi(android.os.Build.VERSION_CODES.M)
@Override
public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
super.onReceivedHttpError(view, request, errorResponse);
// 这个方法在6.0才出现
int statusCode = errorResponse.getStatusCode();
if (ERROR_CODE404 == statusCode || ERROR_CODE500 == statusCode) {
}
}
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
//接受证书
handler.proceed();
}
});
}