webview设置字体颜色、大小

解决方法:
改变 WebView上显示网页内容的CSS样式:
//字体颜色设为白色, “p”标签内的字体颜色  “*”定义了字体大小以及行高;
public final static String CSS_STYLE ="<style>* {font-size:16px;line-height:20px;}p {color:#FFFFFF;}</style>";
//data是要显示的内容
webView.loadDataWithBaseURL(null, CSS_STYLE+data, "text/html","utf-8", null);

比较全的:
public final static String CSS_STYLE =" <style>* {font-size:16px;line-height:20px;} p {color:#333;} a {color:#3E62A6;} img {max-width:310px;}pre {font-size:9pt;line-height:12pt;font-family:Courier New,Arial;border:1px solid #ddd;border-left:5px solid #6CE26C;background:#f6f6f6;padding:5px;}</style>"; 上面全局样式:“*”定义了字体大小以及行高;“p”标签内的字体颜色;“a”标签内的字体颜色;“img”标签的图片最大宽度;“pre”为代码样式;------------------------------------------------------------------------------------------------------------------------------------------------------
借鉴自:http://www.android100.org/html/201306/25/3285.html

其他:
资讯内容是由服务返回的一串带HTML标签的字符串:
String body = newsDetail.getBody();
相关资讯则是由服务返回的数据组装的:
String strRelative = "";
for(Relative relative : newsDetail.getRelatives()){
    strRelative += String.format("%s", relative.url, relative.title);
}
图片处理
WebView上显示图片,不能直接显示大图,这会影响页面的美观以及用户体验,因此要过滤掉原始图片的高宽属性,使用全局的图片样式。同时客户端可以根据用户设置,是否加载图片显示,以达到节省流量的目的。
if(isLoadImage){
    //过滤掉 img标签的width,height属性
    body = body.replaceAll("(webview设置字体颜色]*?)\\s+width\\s*=\\s*\\S+","$1");
    body = body.replaceAll("(webview设置字体颜色]*?)\\s+height\\s*=\\s*\\S+","$1");
}else{
    //过滤掉 img标签
    body = body.replaceAll("<\\s*img\\s+([^>]*)\\s*>","");
}
WebView展示HTML
mWebView.loadDataWithBaseURL(null, body, "text/html", "utf-8",null);
 
  
-----------------------------------------------------------------------------------------
其它方法:WebView webview= (WebView) findViewById(R.id.webview);
WebSettings settings=webview.getSettings();
settings.setTextSize(WebSettings.TextSize.SMALLEST);
settings.setTextSize(WebSettings.TextSize.SMALLER);
settings.setTextSize(WebSettings.TextSize.NORMAL);
settings.setTextSize(WebSettings.TextSize.LARGER);
settings.setTextSize(WebSettings.TextSize.LARGEST);

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值