Skip to content

Commit d2a729b

Browse files
committed
add setCustomWebViewLayout
1 parent e12b026 commit d2a729b

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

ByWebView/src/main/java/me/jingbin/web/ByWebView.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import android.annotation.SuppressLint;
44
import android.app.Activity;
55
import android.content.Intent;
6-
import android.content.pm.ActivityInfo;
76
import android.os.Build;
87
import android.support.annotation.LayoutRes;
98
import 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
*/
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<resources>
2+
<item type="id" name="by_custom_webview"/>
3+
</resources>

app/src/main/java/com/example/jingbin/webviewstudy/ByWebViewActivity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ private void initTitle() {
7474
LinearLayout container = findViewById(R.id.ll_container);
7575
byWebView = ByWebView
7676
.with(this)
77+
.setCustomWebViewLayout(R.layout.layout_costom_webview)
7778
.setWebParent(container, new LinearLayout.LayoutParams(-1, -1))
7879
.useWebProgress(ContextCompat.getColor(this, R.color.coloRed))
7980
.setOnTitleProgressCallback(onTitleProgressCallback)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:id="@+id/by_custom_webview"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent" />
6+

0 commit comments

Comments
 (0)