android中使用WebView加载html字符串

2019-11-09  本文已影响0人  穿越平行宇宙
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView wv = findViewById(R.id.wv);

        StringBuffer sb = new StringBuffer();

        //添加html

        sb.append("<html><head><meta http-equiv='content-type' content='text/html; charset=utf-8'>");

        sb.append("<meta charset='utf-8'  content='1'></head><body style='color: black'><p></p>");

        //< meta http-equiv="refresh"content="time" url="url" >

        //添加文件的内容

//        sb.append("<h5>一.在线支付</h5><p>1.支付宝:支付宝即时到账,方便快捷。</p ><p>2.网上银行:通过网上银行,用户可以享受到方便、快捷、高效和可靠的全方位服务。</p >");
sb.append("</div>\n" +
        "                   <p><span style=\"font-family: '宋体'; font-size: 10.5pt; mso-spacerun: 'yes'\"><o:p></o:p></span><span style=\"font-family: '宋体'; color: rgb(39,39,39); font-size: 10.5pt; mso-spacerun: 'yes'\"><o:p>&nbsp; </o:p></span><span style=\"font-family: '宋体'; color: rgb(39,39,39); font-size: 10.5pt; mso-spacerun: 'yes'\"><o:p>&nbsp;</o:p></span></p >\n" +
        "<p><strong>1.正品承诺:</strong>动品网出售的大部分商品已获得品牌商或总代授权,进货渠道正规,所出售的产品均为正品行货,承诺假一罚二。</p >\n" +
        "<p>&nbsp;<strong><br>\n" +
        "2.平民价格:</strong>动品网在产品定价上以让利顾客为主,更为合理的价格就能让顾客享受到专业体育器材的品质;再加上网站定期会推出&nbsp;厂家让利活动,所以不再议价。</p >\n" +
        "<p><br>\n" +
        "<strong>3.产品色差:</strong>所有产品均为实物拍摄,我们的照片尽可能的与实物颜色保持一致,货品图片颜色大小因拍摄或计算机屏幕设定产生差异会&nbsp;略有不同,以实际货品颜色大小为准。</p >\n" +
        "<p>&nbsp;</p >\n" +
        "<p><strong>4.收货提醒:</strong>动品网使用第三方快递公司发货。您在收到产品包裹时务必当着第三方快递送货员的面拆包验货,确保货品完好。如果发现货品压损,可以直接拒收,然后联系客服中心(<span style=\"color: rgb(64, 64, 64); font-family: arial; font-size: 15px; line-height: 22px; text-align: center;\">010-64663105或者010-56245509</span>)为您重新换发您订购的产品。如没有进行检查货品,事后货品如损坏,动品网将无法赔付。<br>\n" +
        "&nbsp;</p >\n" +
        "<p><strong>5.关于缺货:</strong>动品网所上架的商品在销售过程中可能出现短期缺货的情况,而没有及时下架,造成您的订单中产品缺货的,动品网会及时告知,您可以根据自己的情况进行退款,取消订单或者换货。</p >\n" +
        "<p>&nbsp;<br>\n" +
        "<strong>6.订单取消:</strong>所有订单在客服确认后三天没有付款的订单系统将会自动取消。在使用动品网券下单后,取消订单将导致动品网券失效,则无法返还。</p >\n" +
        "<p>&nbsp;</p >\n" +
        "<p>&nbsp;</p >                  </div>");
        //加载本地文件

        // sb.append("<img src='file:///"+AContext.getFileUtil().getDownloadsPath()+"'>");

        sb.append("</body></html>");


        // webView.loadData(data, mimeType, encoding);

        //设置字符编码,避免乱码

        wv.getSettings().setDefaultTextEncodingName("utf-8");

        wv.loadDataWithBaseURL(null, sb.toString(), "text/html", "utf-8", null);


    }
}

效果如下:

image.png
上一篇 下一篇

猜你喜欢

热点阅读