前言
HTML+原生交互是最近比较流行的方式,很多人都在使用,用了这么久的这种模式,小编#整合了下,方便技术更替之后再次查看,也希望这篇文章能够对你有用。
下面就简单整合下,HTML与原生如何交互
1.布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/comm_bg"
android:orientation="vertical">
<include
android:id="@+id/layout_top_bar"
layout="@layout/top_bar" />
//PtrClassicFrameLayout [下拉刷新框架view]
<com.reach.doooly.pullresh.PtrClassicFrameLayout
xmlns:cube_ptr="http://schemas.android.com/apk/res-auto"
android:id="@+id/view_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/layout_top_bar">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<WebView
android:id="@+id/view_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ProgressBar
android:id="@+id/view_progressbar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="1dp"
android:progressDrawable="@drawable/pull_progressbar_pg"
android:visibility="gone" />
</RelativeLayout>
<include layout="@layout/webview_err_view"
android:visibility="gone"/>
</RelativeLayout>
</com.reach.doooly.pullresh.PtrClassicFrameLayout>
</RelativeLayout>
2.activity
package com.reach.doooly.server;
import android.app.Activity;
import android.os.Bundle;
import com.reach.doooly.R;
/**
* Created by Albert on 2017/10/16.
*/
public class WebViewActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview_layout);
}
}
下面我们需要做的是
[1]初始化webview
private WebView webView;
private void initView(){
webView=(WebView)findViewById(R.id.view_webview);
}
[2]webview提供javascript支持
WebSettings webSettings = webView.getSettings();
webSettings.setTextZoom(100);//设置web端界面不随html改变
webSettings.setDomStorageEnabled(true); // 使用localStorage
webSettings.setLoadWithOverviewMode(true); // 设置页面自适应屏幕
webSettings.setJavaScriptEnabled