33import android .annotation .SuppressLint ;
44import android .app .Activity ;
55import android .content .Intent ;
6- import android .content .pm .ActivityInfo ;
76import android .os .Build ;
87import android .support .annotation .LayoutRes ;
98import android .support .annotation .NonNull ;
@@ -51,7 +50,8 @@ private ByWebView(Builder builder) {
5150
5251 RelativeLayout relativeLayout = new RelativeLayout (activity );
5352 RelativeLayout .LayoutParams layoutParams = new RelativeLayout .LayoutParams (ViewGroup .LayoutParams .MATCH_PARENT , ViewGroup .LayoutParams .MATCH_PARENT );
54- mWebView = new WebView (activity );
53+ // 设置WebView
54+ setWebView (builder .mCustomWebViewId );
5555 relativeLayout .addView (mWebView , layoutParams );
5656 // 进度条布局
5757 handleWebProgress (builder , relativeLayout );
@@ -71,6 +71,21 @@ private ByWebView(Builder builder) {
7171 handleJsInterface (builder );
7272 }
7373
74+ /**
75+ * 配置自定义的WebView
76+ */
77+ private void setWebView (int mCustomWebViewId ) {
78+ if (mCustomWebViewId != 0 ) {
79+ try {
80+ mWebView = LayoutInflater .from (activity ).inflate (mCustomWebViewId , null ).findViewById (R .id .by_custom_webview );
81+ } catch (Exception e ) {
82+ throw new IllegalStateException ("Sorry, ByWebView setWebView() is Error!" );
83+ }
84+ } else {
85+ mWebView = new WebView (activity );
86+ }
87+ }
88+
7489 @ SuppressLint ({"JavascriptInterface" , "AddJavascriptInterface" })
7590 private void handleJsInterface (Builder builder ) {
7691 if (!TextUtils .isEmpty (builder .mInterfaceName ) && builder .mInterfaceObj != null ) {
@@ -304,6 +319,7 @@ public static class Builder {
304319 // 进度条 高度
305320 private int mProgressHeightDp ;
306321 private int mErrorLayoutId ;
322+ private int mCustomWebViewId ;
307323 private String mErrorTitle ;
308324 private String mInterfaceName ;
309325 private Object mInterfaceObj ;
@@ -377,6 +393,14 @@ public Builder useWebProgress(String startColor, String endColor, int heightDp)
377393 return this ;
378394 }
379395
396+ /**
397+ * @param customWebViewId 三方WebView,注意一定要使用id = by_custom_webview
398+ */
399+ public Builder setCustomWebViewLayout (@ LayoutRes int customWebViewId ) {
400+ mCustomWebViewId = customWebViewId ;
401+ return this ;
402+ }
403+
380404 /**
381405 * @param errorLayoutId 错误页面布局,标题默认“网页打开失败”
382406 */
0 commit comments