Android 4.1视口缩放(setInitialScale,meta initial-scale not working) [英] Android 4.1 viewport scaling ( setInitialScale, meta initial-scale not working)

查看:898
本文介绍了Android 4.1视口缩放(setInitialScale,meta initial-scale not working)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在SO和其余的互联网上有数百个帖子试图修复在Android中的视口缩放,我现在相当确定,你只是不能设置初始规模在Android 4.1的网页视图。

There are hundreds of posts across SO and the rest of the internet about trying to fix viewport scaling in Android, and I'm now fairly certain that you just cannot set the initial-scale in a webview on Android 4.1.

我正在建立一个Cordova(Phonegap)应用程序,我已经在iPhone 3GS,4S和5和iPad 2上运行iOS7和iOS6上的3GS

I'm building a Cordova (Phonegap) app, and I've got every thing scaled nicely on the iPhone 3GS, 4S and 5 and an iPad 2, running iOS7 and the 3GS on iOS6

我也将UI缩放到适合Moto G,LG Nexus 5,Google Nexus 5和Samsung Galaxy S4,所有这些都运行Android 4.4

I've also got the UI scaled to fit on the Moto G, LG Nexus 5, Google Nexus 5 and Samsung Galaxy S4, all running Android 4.4

但是在运行Android 4.1的Samsung Galaxy S2和S3 Mini上,我无法设置初始比例。

But on a Samsung Galaxy S2 and S3 Mini running Android 4.1, I cannot set the initial-scale.

HTML视口元标记在4.1或4.4中不起作用(适用于Chrome,不适用于WebView或默认浏览器):

The HTML viewport meta tag doesn't work on 4.1 or 4.4 (works in Chrome, not in the WebView or default browser):

<meta name="viewport" content="user-scalable=no, initial-scale=0.5, width=device-width, height=device-height, target-densitydpi=device-dpi" />

我从cli, cordova创建basicProject ,如果我在主Activity中的onCreate方法中添加一些Java,特别是 setInitialScale 方法:

I've knocked up an absolutely basic Cordova project from the cli, cordova create basicProject, and if I add some Java to the onCreate method in the main Activity, specifically the setInitialScale method:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.init();
    // Set by <content src="index.html" /> in config.xml
    super.loadUrl(Config.getStartUrl());
    //super.loadUrl("file:///android_asset/www/index.html");

    this.appView.setInitialScale( 50  );

}

然后我得到Moto G的4.4缩放比预期:

Then I get the Moto G on 4.4 scaling as expected:

但是在Galaxy S2上运行的同一个项目并没有得到扩展。

But exactly the same project running on the Galaxy S2, does not get scaled.

我只限于使用4.1或4.4上的一系列设备测试三星设备,所以如果任何人都可以在4.2或4.3上测试同样的东西, 4.1

I'm limited to testing on Samsung devices with 4.1, or a range of devices on 4.4, so if anyone can test the same thing on 4.2 or 4.3, or anyone on 4.1 not with a Samsung that'd be helpful.

有谁知道如何让Android 4.1服从 setInitialScale()

Does anyone know how to get Android 4.1 to obey setInitialScale()

推荐答案

您应该能够在网页上添加一些javascript来扩展内容。下面是一个示例,它将缩放您的html内容,以便内容适合屏幕的可用宽度:

You should be able to add some javascript to your webpages that will scale the content when picked up by your webview. Here is an example that will scale your html content so that the content fits to the available width of the screen:

function customScaleThisScreen() 
    var contentWidth = document.body.scrollWidth, 
        windowWidth = window.innerWidth, 
        newScale = windowWidth / contentWidth;
    document.body.style.zoom = newScale;
}

这将适用于较早的+)网络视图。

This will work on older (pre 4.2) and newer chromium-based (4.2+) webviews.

HTHs

这篇关于Android 4.1视口缩放(setInitialScale,meta initial-scale not working)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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