webview加载网页和assets的html文件

来源:互联网 发布:超人软件站 编辑:程序博客网 时间:2024/06/04 20:12


效果


代码

package com.fe.statuslayout;import android.graphics.Color;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.Toolbar;import android.view.View;import android.webkit.WebSettings;import android.webkit.WebView;import android.webkit.WebViewClient;import android.widget.Button;/** * Created by Administrator on 2017/3/6 0006. * webview加载网页和assetshtml文件 */public class MainTwo extends AppCompatActivity implements View.OnClickListener {    private Toolbar tb_bar;    private Button get;    private WebView webview;    private String path = "http://www.baidu.com";    private Button html;    private Button assets_html;    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main_two);        initView();        initToolBar();    }    private void initToolBar() {        //Toast.makeText(this, "1", Toast.LENGTH_SHORT).show();        //Toast.makeText(this, "2", Toast.LENGTH_SHORT).show();        tb_bar.setTitle("Status");        tb_bar.setTitleTextColor(Color.WHITE);        tb_bar.inflateMenu(R.menu.base_toolbar_menu);    }    private void initView() {        tb_bar = (Toolbar) findViewById(R.id.tb_bar);        get = (Button) findViewById(R.id.get);        get.setOnClickListener(this);        webview = (WebView) findViewById(R.id.webview);        webview.setOnClickListener(this);        html = (Button) findViewById(R.id.html);        html.setOnClickListener(this);        assets_html = (Button) findViewById(R.id.assets_html);        assets_html.setOnClickListener(this);    }    @Override    public void onClick(View v) {        switch (v.getId()) {            case R.id.get:                //加载网页                webview.loadUrl(path);                //webview默认使用第三方浏览器打开网页。                //设置webview自己展示网页                webview.setWebViewClient(new WebViewClient() {                    @Override                    public boolean shouldOverrideUrlLoading(WebView view, String url) {                        view.loadUrl(url);                        return true;                    }                });                break;            case R.id.html:                //加载html                StringBuilder sb = new StringBuilder();                //拼接一段HTML代码                sb.append("<html>");                sb.append("<head>");                sb.append("<title>欢迎你</title>");                sb.append("</head>");                sb.append("<body>");                sb.append("<h2>欢迎你访问<a href=\"http://www.crazyit.org\">"                        + "疯狂Java联盟</a></h2>");                sb.append("</body>");                sb.append("</html>");                //使用简单的loadData方法会导致乱码,可能是Android APIBug                //webview.loadData(sb.toString(), "text/html", "utf-8");                webview.loadDataWithBaseURL(null, sb.toString(), "text/html", "utf-8", null);                break;            case R.id.assets_html:                //加载assets目录下的html                //加上下面这段代码可以使网页中的链接不以浏览器的方式打开                webview.setWebViewClient(new WebViewClient());                webview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);//滚动条风格,为0指滚动条不占用空间,直接覆盖在网页上                //得到webview设置                WebSettings webSettings = webview.getSettings();                //允许使用javascript                webSettings.setJavaScriptEnabled(true);                //设置字符编码                webSettings.setDefaultTextEncodingName("UTF-8");                //支持缩放                webSettings.setSupportZoom(true);                webSettings.setBuiltInZoomControls(true);                webSettings.setUseWideViewPort(true);                webSettings.setLoadWithOverviewMode(true);                //WebAppInterfacejavascript绑定                //webview.addJavascriptInterface(new PaymentJavaScriptInterface(),"Android");                //android assets目录下html文件路径url file:///android_asset/profile.html                String url = "file:///android_asset/" + "css_three.html";                webview.loadUrl(url);                break;        }    }    /*private class PaymentJavaScriptInterface {    }*/}

assets目录


布局

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"              android:layout_width="match_parent"              android:layout_height="match_parent"              android:orientation="vertical">    <android.support.v7.widget.Toolbar        android:id="@+id/tb_bar"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:theme="@style/Theme.ToolBar.Base"/>    <Button        android:id="@+id/get"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="加载网页"/>    <Button        android:id="@+id/html"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="加载html"/>    <Button        android:id="@+id/assets_html"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="加载assets目录下的html"/>    <WebView        android:id="@+id/webview"        android:layout_width="match_parent"        android:layout_height="match_parent"/></LinearLayout>


AndroidManifest.xml

<!--网络权限--><uses-permission android:name="android.permission.INTERNET"/>

。。。

0 0