Skip to content

Commit 700a794

Browse files
committed
add 与ToolBar联动,可设置自定义WebView
1 parent ab099ff commit 700a794

File tree

17 files changed

+604
-39
lines changed

17 files changed

+604
-39
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ static boolean isNetworkConnected(Context context) {
136136
}
137137
}
138138

139+
static int dip2px(Context context, float dpValue) {
140+
final float scale = context.getResources().getDisplayMetrics().density;
141+
return (int) (dpValue * scale + 0.5f);
142+
}
143+
139144
public static String getUrl(String url) {
140145
String urlResult = "";
141146
if (TextUtils.isEmpty(url)) {

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

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import android.view.ViewGroup;
1515
import android.webkit.WebSettings;
1616
import android.webkit.WebView;
17+
import android.widget.FrameLayout;
1718
import 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

ByWebView/src/main/res/values/ids.xml

Lines changed: 0 additions & 3 deletions
This file was deleted.

app/src/main/AndroidManifest.xml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
</intent-filter>
4949
</activity>
5050
<activity
51-
android:name=".WebViewActivity"
51+
android:name=".ui.WebViewActivity"
5252
android:configChanges="orientation|screenSize"
5353
android:hardwareAccelerated="true"
5454
android:launchMode="singleTask"
@@ -58,7 +58,7 @@
5858

5959

6060
<!--用于DeepLink,html跳到此页面 scheme_Adr: 'will://link/testid',-->
61-
<activity android:name=".DeepLinkActivity">
61+
<activity android:name=".ui.DeepLinkActivity">
6262
<intent-filter>
6363
<action android:name="android.intent.action.VIEW" />
6464

@@ -87,7 +87,7 @@
8787
android:required="false" />
8888

8989
<activity
90-
android:name=".ByWebViewActivity"
90+
android:name=".ui.ByWebViewActivity"
9191
android:configChanges="orientation|screenSize"
9292
android:hardwareAccelerated="true"
9393
android:launchMode="singleTask"
@@ -106,7 +106,13 @@
106106
</intent-filter>
107107

108108
</activity>
109-
109+
<activity
110+
android:name=".ui.CoordinatorWebActivity"
111+
android:configChanges="orientation|screenSize"
112+
android:hardwareAccelerated="true"
113+
android:screenOrientation="portrait"
114+
android:theme="@style/WebViewTheme"
115+
tools:ignore="LockedOrientationActivity" />
110116
</application>
111117

112118
</manifest>

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import android.widget.Toast;
2020

2121
import com.example.jingbin.webviewstudy.tencentx5.X5WebViewActivity;
22+
import com.example.jingbin.webviewstudy.ui.ByWebViewActivity;
23+
import com.example.jingbin.webviewstudy.ui.CoordinatorWebActivity;
2224
import com.example.jingbin.webviewstudy.utils.StatusBarUtil;
2325

2426
import me.jingbin.web.ByWebTools;
@@ -53,6 +55,7 @@ private void initView() {
5355
findViewById(R.id.bt_upload_photo).setOnClickListener(this);
5456
findViewById(R.id.bt_call).setOnClickListener(this);
5557
findViewById(R.id.bt_java_js).setOnClickListener(this);
58+
findViewById(R.id.bt_toolbar).setOnClickListener(this);
5659

5760
rbSystem = findViewById(R.id.rb_system);
5861
etSearch = findViewById(R.id.et_search);
@@ -108,6 +111,9 @@ public void onClick(View v) {
108111
String deepLinkUrl = "file:///android_asset/deeplink.html";
109112
loadUrl(deepLinkUrl, getString(R.string.deeplink));
110113
break;
114+
case R.id.bt_toolbar:// 与ToolBar联动,自定义WebView
115+
CoordinatorWebActivity.loadUrl(this, "http://www.baidu.com", "百度一下", 0);
116+
break;
111117
case R.id.tv_version:
112118
AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
113119
builder.setTitle("感谢");

app/src/main/java/com/example/jingbin/webviewstudy/ByWebViewActivity.java renamed to app/src/main/java/com/example/jingbin/webviewstudy/ui/ByWebViewActivity.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.example.jingbin.webviewstudy;
1+
package com.example.jingbin.webviewstudy.ui;
22

33
import android.content.Context;
44
import android.content.Intent;
@@ -17,6 +17,8 @@
1717
import android.widget.TextView;
1818
import android.widget.Toast;
1919

20+
import com.example.jingbin.webviewstudy.MainActivity;
21+
import com.example.jingbin.webviewstudy.R;
2022
import com.example.jingbin.webviewstudy.config.MyJavascriptInterface;
2123
import com.example.jingbin.webviewstudy.utils.StatusBarUtil;
2224
import com.example.jingbin.webviewstudy.utils.WebTools;
@@ -74,7 +76,6 @@ private void initTitle() {
7476
LinearLayout container = findViewById(R.id.ll_container);
7577
byWebView = ByWebView
7678
.with(this)
77-
.setCustomWebViewLayout(R.layout.layout_custom_webview)
7879
.setWebParent(container, new LinearLayout.LayoutParams(-1, -1))
7980
.useWebProgress(ContextCompat.getColor(this, R.color.coloRed))
8081
.setOnTitleProgressCallback(onTitleProgressCallback)

0 commit comments

Comments
 (0)