1414import android .view .ViewGroup ;
1515import android .webkit .WebSettings ;
1616import android .webkit .WebView ;
17+ import android .widget .FrameLayout ;
1718import android .widget .RelativeLayout ;
1819
1920/**
@@ -48,14 +49,17 @@ private ByWebView(Builder builder) {
4849 this .mErrorTitle = builder .mErrorTitle ;
4950 this .mErrorLayoutId = builder .mErrorLayoutId ;
5051
51- RelativeLayout relativeLayout = new RelativeLayout (activity );
52- RelativeLayout .LayoutParams layoutParams = new RelativeLayout .LayoutParams (ViewGroup .LayoutParams .MATCH_PARENT , ViewGroup .LayoutParams .MATCH_PARENT );
52+ FrameLayout parentLayout = new FrameLayout (activity );
5353 // 设置WebView
54- setWebView (builder .mCustomWebViewId );
55- relativeLayout .addView (mWebView , layoutParams );
54+ setWebView (builder .mCustomWebView );
55+ parentLayout .addView (mWebView , new FrameLayout . LayoutParams ( ViewGroup . LayoutParams . MATCH_PARENT , ViewGroup . LayoutParams . MATCH_PARENT ) );
5656 // 进度条布局
57- handleWebProgress (builder , relativeLayout );
58- builder .mWebContainer .addView (relativeLayout , builder .mLayoutParams );
57+ handleWebProgress (builder , parentLayout );
58+ if (builder .mIndex != -1 ) {
59+ builder .mWebContainer .addView (parentLayout , builder .mIndex , builder .mLayoutParams );
60+ } else {
61+ builder .mWebContainer .addView (parentLayout , builder .mLayoutParams );
62+ }
5963 // 配置
6064 handleSetting ();
6165 // 视频、照片、进度条
@@ -74,13 +78,9 @@ private ByWebView(Builder builder) {
7478 /**
7579 * 配置自定义的WebView
7680 */
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- }
81+ private void setWebView (WebView mCustomWebView ) {
82+ if (mCustomWebView != null ) {
83+ mWebView = mCustomWebView ;
8484 } else {
8585 mWebView = new WebView (activity );
8686 }
@@ -147,7 +147,7 @@ public void setTextZoom(int textZoom) {
147147 mWebView .getSettings ().setTextZoom (textZoom );
148148 }
149149
150- private void handleWebProgress (Builder builder , RelativeLayout relativeLayout ) {
150+ private void handleWebProgress (Builder builder , FrameLayout parentLayout ) {
151151 if (builder .mUseWebProgress ) {
152152 mProgressBar = new WebProgress (activity );
153153 if (builder .mProgressStartColor != 0 && builder .mProgressEndColor != 0 ) {
@@ -161,11 +161,13 @@ private void handleWebProgress(Builder builder, RelativeLayout relativeLayout) {
161161 && TextUtils .isEmpty (builder .mProgressEndColorString )) {
162162 mProgressBar .setColor (builder .mProgressStartColorString , builder .mProgressStartColorString );
163163 }
164+ int progressHeight = ByWebTools .dip2px (parentLayout .getContext (), WebProgress .WEB_PROGRESS_DEFAULT_HEIGHT );
164165 if (builder .mProgressHeightDp != 0 ) {
165166 mProgressBar .setHeight (builder .mProgressHeightDp );
167+ progressHeight = ByWebTools .dip2px (parentLayout .getContext (), builder .mProgressHeightDp );
166168 }
167169 mProgressBar .setVisibility (View .GONE );
168- relativeLayout .addView (mProgressBar );
170+ parentLayout .addView (mProgressBar , new FrameLayout . LayoutParams ( ViewGroup . LayoutParams . MATCH_PARENT , progressHeight ) );
169171 }
170172 }
171173
@@ -262,15 +264,15 @@ public WebProgress getProgressBar() {
262264 public void showErrorView () {
263265 try {
264266 if (mErrorView == null ) {
265- RelativeLayout parent = (RelativeLayout ) mWebView .getParent ();
267+ FrameLayout parent = (FrameLayout ) mWebView .getParent ();
266268 mErrorView = LayoutInflater .from (parent .getContext ()).inflate ((mErrorLayoutId == 0 ) ? R .layout .by_load_url_error : mErrorLayoutId , null );
267269 mErrorView .setOnClickListener (new View .OnClickListener () {
268270 @ Override
269271 public void onClick (View v ) {
270272 reload ();
271273 }
272274 });
273- parent .addView (mErrorView , new RelativeLayout .LayoutParams (ViewGroup .LayoutParams .MATCH_PARENT , ViewGroup .LayoutParams .MATCH_PARENT ));
275+ parent .addView (mErrorView , new FrameLayout .LayoutParams (ViewGroup .LayoutParams .MATCH_PARENT , ViewGroup .LayoutParams .MATCH_PARENT ));
274276 } else {
275277 mErrorView .setVisibility (View .VISIBLE );
276278 }
@@ -319,8 +321,9 @@ public static class Builder {
319321 // 进度条 高度
320322 private int mProgressHeightDp ;
321323 private int mErrorLayoutId ;
322- private int mCustomWebViewId ;
324+ private int mIndex = - 1 ;
323325 private String mErrorTitle ;
326+ private WebView mCustomWebView ;
324327 private String mInterfaceName ;
325328 private Object mInterfaceObj ;
326329 private ViewGroup mWebContainer ;
@@ -346,6 +349,20 @@ public Builder setWebParent(@NonNull ViewGroup webContainer, ViewGroup.LayoutPar
346349 return this ;
347350 }
348351
352+ /**
353+ * WebView容器
354+ *
355+ * @param webContainer 外部WebView容器
356+ * @param index 加入的位置
357+ * @param layoutParams 对应的LayoutParams
358+ */
359+ public Builder setWebParent (@ NonNull ViewGroup webContainer , int index , ViewGroup .LayoutParams layoutParams ) {
360+ this .mWebContainer = webContainer ;
361+ this .mIndex = index ;
362+ this .mLayoutParams = layoutParams ;
363+ return this ;
364+ }
365+
349366 /**
350367 * @param isUse 是否使用进度条,默认true
351368 */
@@ -394,10 +411,10 @@ public Builder useWebProgress(String startColor, String endColor, int heightDp)
394411 }
395412
396413 /**
397- * @param customWebViewId 三方WebView,注意一定要使用id = by_custom_webview
414+ * @param customWebView 自定义的WebView
398415 */
399- public Builder setCustomWebViewLayout ( @ LayoutRes int customWebViewId ) {
400- mCustomWebViewId = customWebViewId ;
416+ public Builder setCustomWebView ( WebView customWebView ) {
417+ mCustomWebView = customWebView ;
401418 return this ;
402419 }
403420
0 commit comments