webview中的3g页面字体问题

分享下最近遇到的问题

最近开发一个3G页面,遇到一个问题,嵌套在SamgSang Note4的webview中,不管怎么设置基准字体,字体设置多小都没有用,一直都显示很大。 试了各种可能:

最后是客户端在他们的包里加了几句话就行了


settings.setJavaScriptEnabled(true);
settings.setBuiltInZoomControls(true);
settings.setUseWideViewPort(true);
settings.setLoadWithOverviewMode(true);

手机调试

https://developer.chrome.com/devtools/docs/remote-debugging

需要翻墙可以安装chorme的插件:[Proxy Switchy]详细介绍https://github.com/lzhengms/goagent,也可以使用翻墙软件fgp.exe

最终的解决方案:

1.设备像素比

张鑫旭的文章

http://www.zhangxinxu.com/wordpress/2012/08/window-devicepixelratio/

http://www.zhangxinxu.com/wordpress/2012/10/new-pad-retina-devicepixelratio-css-page/

2.淘宝的可伸缩布局方案

https://github.com/amfe/lib.flexible

3.手机端的页面:

http://m.taobao.com

4.淘宝的代码:


// flexible.js
    !function(a) {
        var c, d, e, f = a.document, g = f.documentElement, h = f.querySelector('meta[name="viewport"]'), i = f.querySelector('meta[name="flexible"]');

        function b() {
            var b = g.getBoundingClientRect().width;
            b / c > 540 && (b = 540 * c), a.rem = b / 16, g.style.fontSize = a.rem + "px"
        }
        if (h) {
            //console.warn("将根据已有的meta标签来设置缩放比例");
            var j = h.getAttribute("content").match(/initial\-scale=(["']?)([\d\.]+)\1?/);
            j && (d = parseFloat(j[2]), c = parseInt(1 / d))
        } else if (i) {
            var j = i.getAttribute("content").match(/initial\-dpr=(["']?)([\d\.]+)\1?/);
            j && (c = parseFloat(j[2]), d = parseFloat((1 / c).toFixed(2)))
        }
        if (!c && !d) {
            var k = (a.navigator.appVersion.match(/android/gi), a.navigator.appVersion.match(/iphone/gi)),
                    c = a.devicePixelRatio;
            c = k ? c >= 3 ? 3 : c >= 2 ? 2 : 1 : 1, d = 1 / c;
        }
        if (g.setAttribute("data-dpr", c), !h)
            if (h = f.createElement("meta"), h.setAttribute("name", "viewport"), h.setAttribute("content", "initial-scale=" + d + ", maximum-scale=" + d + ", minimum-scale=" + d + ", user-scalable=no"), g.firstElementChild)
                g.firstElementChild.appendChild(h);
            else {
                var l = f.createElement("div");
                l.appendChild(h), f.write(l.innerHTML)
            }
        a.dpr = c, a.addEventListener("resize", function() {
            clearTimeout(e), e = setTimeout(b, 300)
        }, !1), a.addEventListener("pageshow", function(a) {
            a.persisted && (clearTimeout(e), e = setTimeout(b, 300))
        }, !1), "complete" === f.readyState ? f.body.style.fontSize = 16 * c + "px" : f.addEventListener("DOMContentLoaded", function() {
            f.body.style.fontSize = 16 * c + "px"
        }, !1), b()
    }(window);

    !function() {
        var a = '@charset "UTF-8";blockquote,body,dd,dl,figure,form,h1,h2,h3,h4,h5,h6,input,ol,p,pre,textarea,ul{margin:0}button,input,ol,td,textarea,th,ul{padding:0}ol,ul{list-style:none}img{max-width:100%}a{text-decoration:none;color:#00a0e9}body{background-color:#E8E8E8;font-family:"Helvetica Neue",Helvetica,STHeiti,Arial,sans-serif;color:#000;-webkit-tap-highlight-color:rgba(0,0,0,0);-webkit-text-size-adjust:100%}.clearfix:after,.clearfix:before{content:"";display:table;clear:both}.hide{display:none}.fr{float:right}.tabs{background-color:#5F6065;color:#ADAEB2}.tab{text-align:center;width:50%;float:left;height:45px;line-height:45px;cursor:pointer}.tab span{color:#BF4D55}.tab.active{color:#D8B766;border-bottom:3px solid #d8b766}.org{position:relative;cursor:pointer}.org .sub-orgs,.org .sub-users{display:none}.org.active>.sub-orgs,.org.active>.sub-users{display:block}.org .arrow{position:absolute;border:5px solid #000;border-color:transparent transparent transparent #000;top:13px}.org.active>.arrow{border-color:#000 transparent transparent}.org .org-name{border-bottom:1px solid #DBDBDB}.depth-1 .org-name{padding:10px 0 10px 20px}.depth-1 .arrow{left:5px}.depth-2 .org-name{padding:10px 0 10px 30px}.depth-2 .arrow{left:15px}.depth-3 .org-name{padding:10px 0 10px 40px}.depth-3 .arrow{left:25px}.depth-4 .org-name{padding:10px 0 10px 50px}.depth-4 .arrow{left:35px}.depth-5 .org-name{padding:10px 0 10px 60px}.depth-5 .arrow{left:45px}.depth-6 .org-name{padding:10px 0 10px 70px}.depth-6 .arrow{left:55px}.depth-7 .org-name{padding:10px 0 10px 80px}.depth-7 .arrow{left:65px}.depth-8 .org-name{padding:10px 0 10px 90px}.depth-8 .arrow{left:75px}.depth-9 .org-name{padding:10px 0 10px 100px}.depth-9 .arrow{left:85px}.depth-10 .org-name{padding:10px 0 10px 110px}.depth-10 .arrow{left:95px}.user{position:relative;padding:10px;border-bottom:1px solid #DBDBDB;background-color:#F9F9F9}.user .user-avatar{width:10%}.user img{width:100%;border-radius:50%}.user .contact-way,.user .username{position:absolute}.user .username{left:15%;top:32%;text-overflow:ellipsis;overflow:hidden;white-space:nowrap;width:45%}.user .contact-way{width:10%;height:100%;top:0}.user .contact-mobile{right:10%}.user .contact-sms{right:25%}.user .sms{background:url(../images/sms.png) no-repeat center;background-size:100%;display:block;width:100%;height:100%}.user .mobilephone{background:url(../images/phone.png) no-repeat center;background-size:100%;display:block;width:100%;height:100%}',
                b = document.createElement("style");
        if (document.getElementsByTagName("head")[0].appendChild(b), b.styleSheet)
            b.styleSheet.disabled || (b.styleSheet.cssText = a);
        else
            try {
                b.innerHTML = a
            } catch (c) {
                b.innerText = a
            }
    }();