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>