webview的属性
//支持javascript
web.getSettings().setJavaScriptEnabled(true);
//不使用缓存
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
// 设置可以支持缩放
web.getSettings().setSupportZoom(true);
// 设置出现缩放工具
web.getSettings().setBuiltInZoomControls(true);
//扩大比例的缩放
web.getSettings().setUseWideViewPort(true);
// 缩放至屏幕的大小 ,自由缩放 ,隐藏缩放提示
webSettings.setLoadWithOverviewMode(true);
webSettings.setSupportZoom(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setDisplayZoomControls(false);
//支持自动加载图片
webSettings.setLoadsImagesAutomatically(true);
//设置编码格式
webSettings.setDefaultTextEncodingName("utf-8");
//自适应屏幕
web.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
web.getSettings().setLoadWithOverviewMode(true);
点击手机上的返回键时,停止html页面中音乐的播放
@Override
protected void onPause() {
super.onPause();
_webView.reload ();
}
举个例子,基于MVP架构,涉及到Dagger2,不熟悉的同学可忽略
VIEW 层
public class FormAct extends BaseActivity implements FormContract.View {
@Inject
FormPresenter presenter;
@Inject
ApiService apiService;
@BindView(R.id.title_tv)
TextView titleTv;
@BindView(R.id.title_img_back)
ImageView titleImgBack;
@BindView(R.id.form_web)
WebView formWeb;
@BindView(R.id.form_prog)
ProgressBar progressBar;
@Override
protected void onCreate() {
setContentView(R.layout.activity_form);
ButterKnife.bind(this);
}
@Override
protected void initInject() {
DaggerApiComponent.builder().apiModule(new ApiModule(this)).build().inject(this);
}
@Override
protected void initListener() {
apiService.back(this, "报表", titleImgBack, titleTv);
presenter.loadUrl();
}
@Override
public WebView getWeb() {
return formWeb;
}
@Override
public void progressbarVis() {
progressBar.setVisibility(View.VISIBLE);
}
@Override
public void progressbarGone() {
progressBar.setVisibility(View.GONE);
}
@Override
public ProgressBar getProg() {
return progressBar;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (formWeb.canGoBack()) {
formWeb.goBack();
return true;
} else {
finish();
}
}
return super.onKeyDown(keyCode, event);
}
}
Presenter 层
public class FormPresenter implements FormContract.Presenter {
private FormContract.View act;
private FormModel model;
public FormPresenter(FormContract.View act, FormModel formModel) {
this.act = act;
this.model = formModel;
}
@Override
public void loadUrl() {
WebView webView = act.getWeb();
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
view.setVisibility(View.GONE);
}
});
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
if (newProgress == 100) {
act.progressbarGone();
} else {
act.progressbarVis();
act.getProg().setProgress(newProgress);
}
}
});
webView.loadUrl(model.getUrlData());
}
}
Modle层
public class FormModel implements FormContract.Model {
@Override
public String getUrlData() {
return "http://www.baidu.com";
}
}
布局xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.allen.cpsc_ctm.Act.FormAct">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/main_tv_title"
android:orientation="vertical">
<include layout="@layout/activity_title_layout" />
</FrameLayout>
<ProgressBar
android:id="@+id/form_prog"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="3dip"
android:progressDrawable="@drawable/progressbar"
android:visibility="gone" />
<WebView
android:id="@+id/form_web"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
progressbar 文件
?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<corners android:radius="2dp" />
<gradient
android:angle="270"
android:centerColor="#E3E3E3"
android:endColor="#E6E6E6"
android:startColor="#C8C8C8" />
</shape>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="2dp" />
<gradient
android:centerColor="#4AEA2F"
android:endColor="#31CE15"
android:startColor="#5FEC46" />
</shape>
</clip>
</item>
</layer-list>