android webview

webview是指用网页(本地html和在线html)的方式去展示给用户所需要看的内容

在这里介绍webview的两种方式

一 loadUrl

1)权限是必须加的

<uses-permission android:name="android.permission.INTERNET" />

2)在activity中加入控制

这里面有两种方式:

方式一:不使用xml布局,直接在程序中写

//实例化WebView对象 
        webview = new WebView(this); 
        //设置WebView属性,能够执行Javascript脚本 
        webview.getSettings().setJavaScriptEnabled(true); 
        //加载需要显示的网页 
        webview.loadUrl("http://www.baidu.com/"); //或者http://m.baidu.com/这是手机版的,效果大家可以看一下,区别是布局
        //设置Web视图 
        setContentView(webview); 

方式二:使用布局

mail.xml:

<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"
    tools:context=".MainActivity" >

<WebView  
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
      />

</LinearLayout>

Activity:

setContentView(R.layout.main); 
  webview = (WebView) findViewById(R.id.webview); 
//设置WebView属性,能够执行Javascript脚本 
webview.getSettings().setJavaScriptEnabled(true); 
//加载需要显示的网页 
webview.loadUrl("http://www.baidu.com/"); //或者http://m.baidu.com/这是手机版的,效果大家可以看一下,区别是布局

二 loadData

1)权限是必须加的

<uses-permission android:name="android.permission.INTERNET" />

2)在activity中加入控制(这里是否使用xml布局参考上面的,我这里不使用xml)

//实例化WebView对象 
        webview = new WebView(this); 
        //设置WebView属性,能够执行Javascript脚本 
        webview.getSettings().setJavaScriptEnabled(true); 
      String htmlString = openAssetsFile("web/web_article.html");//"web/web_articletest.html"这个地址是
htmlString = htmlString.replace("{title}", "1111");//这是替换web_articletest.html某一个字段,这里是为了修改显示内容
//webview.loadData(htmlString, "text/html", "utf-8");//这种方法会导致显示效果失真
webview.loadDataWithBaseURL(null,htmlString, "text/html",  "utf-8", null);//这种方法会避免显示效果失真

这里提供读取assets文件的方法

/**
     * 读取Assets文件夹中的文件
     * 
     * @param fileName
     * @return
     */
    public String openAssetsFile(String fileName) {
        String content = null;
 
        try {
            // 打开assets文件夹中的文件
            InputStream input = this.getResources().getAssets().open(fileName);
 
            int len = 0;
            // 实现输出流
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            // 读取文件内容
            while ((len = input.read()) != -1) {
                // 将指定的字节写入此byte数组输出流
                out.write(len);
            }
            // 以byte数组形式返回此输出流的当前内容
            byte[] buff = out.toByteArray();
            out.close();
            input.close();
 
            content = new String(buff, "UTF-8");
 
        } catch (IOException e) {
            e.printStackTrace();
        }
 
        return content;
    }


web_andr.css(设置格式)

@charset "utf-8";
*{ margin:0px auto; padding:0px;}
html,body { width:100%}
html { min-width:320px; -webkit-tap-highlight-color:rgba(0,0,0,0)}
body,nav,dl,dt,dd,p,h1,h2,h3,h4,ul,li,input,button,footer,video { margin:0; padding:0;}
body { font:14px/20px "Microsoft YaHei","Lucida Grande",Helvetica,Arial,Verdana; color:#333; background:#f8f9f9}
/* 
header 通用 --- 简单描述
require: 无    --- 依赖(可选)
*/
.fl{ float:left;}
.fr{ float:right;}
.clear{ clear:both;}
/* 
header 通知详情 --- 简单描述
require: 无    --- 依赖(可选)
*/
.notice_con{ background:#f2f6f8; padding:10px; font-size:12px; line-height:24px; color:#000;}
.notice_con h2{ font-size:14px; color:#000; line-height:28px; text-align:justify;text-justify:inter-ideograph;}
.source_box{ border-bottom:solid 1px #d9d9d9; padding-bottom:5px; margin-bottom:5px; line-height:24px; font-size:12px; color:#999;}
.notice_con_box{ margin-bottom:10px; display:block;}
.notice_con_box p{ text-indent:2em; text-align:justify;text-justify:inter-ideograph;}

web_articletest.html

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1">
<title></title>
<link href="file:///android_asset/web/web_andr.css" type="text/css" rel="stylesheet" />
</head>
<body>
   <div class="notice_con">
    <h2>
{title}
</h2>
<div class="source_box">
<span class="fr">{time}</span>
{from}
</div>
<div class="notice_con_box">
            {content}
</div>
   </div>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值