Android Webview - 网页应适合设备屏幕 [英] Android Webview - Webpage should fit the device screen

查看:28
本文介绍了Android Webview - 网页应适合设备屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了以下方法来根据设备屏幕尺寸来适应网页.

I have tried the following to fit the webpage based on the device screen size.

mWebview.setInitialScale(30);

然后设置元数据视口

<meta name="viewport" content="width=320, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; minimum-scale=1.0; user-scalable=0;"/>
<meta name="viewport" content="width=device-width, target-densityDpi=medium-dpi"/>

但没有任何效果,网页未固定到设备屏幕尺寸.

But nothing works, webpage is not fixed to the device screen size.

谁能告诉我如何得到这个?

Can anyone tell me how to get this?

推荐答案

你必须手动计算需要使用的比例,而不是设置为 30.

You have to calculate the scale that you need to use manually, rather than setting to 30.

private int getScale(){
    Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 
    int width = display.getWidth(); 
    Double val = new Double(width)/new Double(PIC_WIDTH);
    val = val * 100d;
    return val.intValue();
}

然后使用

WebView web = new WebView(this);
web.setPadding(0, 0, 0, 0);
web.setInitialScale(getScale());

这篇关于Android Webview - 网页应适合设备屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆