继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

如何使用webview加载一个本地的Html文件

nickcau
关注TA
已关注
手记 113
粉丝 6508
获赞 303

这个功能对于展示app的使用帮助是很有帮助的

https://img1.mukewang.com/5c0cdaf800019f6106381114.jpg

核心代码:

 private void initView() {
        mWebView = (WebView) findViewById(R.id.load_webView);
        mProgressBar = (ProgressBar) findViewById(R.id.load_progressBar);
        mLoadingLayout = (RelativeLayout) findViewById(R.id.loading_layout);
        mPrompt = (TextView) findViewById(R.id.prompt);
        mNoInternetImageView = (ImageView) findViewById(R.id.no_internet_img);

        WebSettings webSettings = mWebView.getSettings();
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH);
        webSettings.setPluginState(WebSettings.PluginState.ON);
        webSettings.setLoadsImagesAutomatically(true);
        webSettings.setUseWideViewPort(true);
        //   webSettings.setBuiltInZoomControls(true);
        mWebView.requestFocus();
        mWebView.loadUrl("file:///android_asset/software_licenses.html");
        initWebViewListener();


    }

    private void initWebViewListener() {
//            this.mWebView.setWebViewClient(new WebViewClient() {
//        });

        this.mWebView.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress >= 80) {
                    mWebView.setVisibility(View.VISIBLE);
                }
            }
        });

    }

本地的html文件是:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes"/>
    <meta name="HandheldFriendly" content="true"/>
    <meta http-equiv="Cache-control" content="no-cache"/>
    <title>Software License</title>
    <style>
        .box { overflow:hidden; width:96%; margin: 0 auto; padding-top: 20px;}
        .lan { overflow:hidden; margin-bottom:20px; color:#2f2f2f; background-color:#eee;}
        h1,
        h2,
        h3,
        h4,
        h5,
        h6,
        p,
        blockquote {
        margin: 0;
        padding: 0;
        text-align: left;
        }
        body {
        font-family: sans, Arial, sans-serif;
        font-size: 13px;
        line-height: 18px;
        color: #2f2f2f;
        background-color: white;
        margin:0;
        padding: 0;
        text-align: center;
        }
        a {
        color: #0069d6;
        }
        a:hover {
        color: #0050a3;
        text-decoration: none;
        }
        a img {
        border: none;
        }
        p {
        overflow:hidden;
        padding:10px 0 0 10px;
        margin-bottom: 9px;
        }
        h1 {
        margin-bottom: 18px;
        font-size: 18pt;
        text-decoration:underline;
        }
        h2 {
        font-size: 24px;
        }
        h3 {
        font-size: 18px;
        }
        h4 {
        font-size: 20px;
        }
        h5 {
        font-size: 14px;
        }
        h6 {
        font-size: 13px;
        }
        hr {
        margin: 0 0 19px;
        border: 0;
        border-bottom: 1px solid #ccc;
        }
        ol, ul { margin:0 10px 0 0; text-align: left;}
        li { text-align: left;}

        /*
        @media screen and (min-width: 914px) {
        body {
        width: 854px;
        margin:10px auto;
        }
        }
        @media print {
        body,code,pre code,h1,h2,h3,h4,h5,h6 {
        color: black;
        }
        table, pre {
        page-break-inside: avoid;
        }
        }
        */
    </style>
</head>
<body>
<div class="box">

<h1>
    apache-mime4j
</h1>

<div class="lan">
    <p>
        Copyright © 2004-2012 The Apache Software Foundation. All Rights Reserved.
    </p>

    <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
        with the License. You may obtain a copy of the License at </p>

    <p>http://www.apache.org/licenses/LICENSE-2.0</p>

    <p>Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
        on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
        for the specific language governing permissions and limitations under the License.</p>

</div>

<h1>
    bcprov-jdk15on
</h1>

<div class="lan">
    <p>
        Copyright © 2000 - 2013 The Legion of the Bouncy Castle Inc. (http://www.bouncycastle.org)
    </p>

    <p>Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
        documentation files (the "Software"), to deal in the Software without restriction, including without limitation
        the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
        to permit persons to whom the Software is furnished to do so, subject to the following conditions: </p>

    <p>The above copyright notice and this permission notice shall be included in all copies or substantial portions of
        the Software.</p>

    <p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
        THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
        CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
        DEALINGS IN THE SOFTWARE.</p>

</div>

</div>
</body>
</html>

代码在https://github.com/nickgao1986/StepSport

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP