android webView工具类

package com.youche.cango.webview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

/**
 * webView工具类
 * Created by cango on 2017/1/15.
 */
public class WebViewUtils extends WebView{

    public WebViewUtils(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public WebViewUtils(Context context) {
        super(context);
    }

    public WebViewUtils(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    /**
     * 初始化webview
     * @param url
     * @param openWay:true :在webview打开,false在手机默认浏览器打开
     */
    public void initWebView(final ProgressBar progressBar,final String url, final boolean openWay){

        this.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    progressBar.setVisibility(View.INVISIBLE);
                } else {
                    if (View.INVISIBLE == progressBar.getVisibility()) {
                        progressBar.setVisibility(View.VISIBLE);
                    }
                    progressBar.setProgress(newProgress);
                }
                super.onProgressChanged(view, newProgress);
            }

        });
        this.post(new Runnable() {
            @Override
            public void run() {
                WebViewUtils.this.loadUrl(url);
            }
        });
        this.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                view.loadUrl(url);
                return openWay;
            }
        });
    }
}

2.布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dialog_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <TextView
        android:id="@+id/dialog_title"
        android:layout_width="@dimen/space_345"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginBottom="@dimen/space_12"
        android:layout_marginTop="@dimen/space_16"
        android:text="标题"
        android:textColor="@color/color_32353A"
        android:textSize="16sp" />

    <View
        android:layout_width="match_parent"
        android:layout_height="@dimen/space_1"
        android:background="@color/CG_base_line" />

    <ProgressBar
        android:id="@+id/progressbar_preview"
        android:layout_width="match_parent"
        android:layout_height="@dimen/space_2"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:progressDrawable="@drawable/progress_horizontal"/>

    <com.youche.cango.webview.WebViewUtils
        android:id="@+id/webview_contract"
        android:layout_width="match_parent"
        android:layout_height="@dimen/space_345"
        android:layout_marginBottom="@dimen/space_15"
        android:background="@color/white"
        android:gravity="center" />



</LinearLayout>

3.进度条样式:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@android:id/background"
        >
        <clip>
            <shape>
                <solid android:color="@color/lemonyellow"></solid>
            </shape>

        </clip>
    </item>
    <item
        android:id="@android:id/secondaryProgress"
        >
        <clip>
            <shape>
                <solid android:color="@color/black"></solid>
            </shape>

        </clip>
    </item>
    <item
        android:id="@android:id/progress"
        >
        <clip>
            <shape>
                <solid android:color="@color/CG_text_bar_on"></solid>
            </shape>

        </clip>
    </item>
</layer-list>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值