1. 程式人生 > >android中Scrollview嵌套WebView問題

android中Scrollview嵌套WebView問題

使用方法 mos http enable att handler 代碼 width stat

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();
            }
        });
    }



android中Scrollview嵌套WebView問題