几年前,我开发了一个 cordova/phonegap 应用程序。我需要将屏幕宽度固定为 1200(某些设备的宽度为 962px),所以我添加了一个元标记来设置视口(viewport)内容宽度,例如
<meta name="viewport" content="width=1200, user-scalable-no">
最近我不得不添加一些新功能并升级到最新的 cordova/phonegap 版本 (7.0.1/6.5.0),而且视口(viewport)元标记似乎被忽略了。运行旧版本我可以在 chrome devtools 中看到 html 宽度为 1200px 但新版本 html 宽度保持为 960px
关于如何解决这个问题有什么想法吗?
最佳答案
我终于可以通过编辑 android 平台文件夹中的 MainActivity.java 文件来解决这个问题。 该文件声明了一个类,该类继承自 CordovaActivity 并覆盖了 onCreate 方法。现在似乎需要进行一些设置,以便 webview 使用“视口(viewport)”元数据。
我在 MainActivity.java 中的 onCreate 方法现在看起来像:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// enable Cordova apps to be started in the background
Bundle extras = getIntent().getExtras();
if (extras != null && extras.getBoolean("cdvStartInBackground", false)) {
moveTaskToBack(true);
}
// Set by <content src="index.html" /> in config.xml
if(appView == null)
init();
WebView webView = (WebView)appView.getView();
WebSettings settings = webView.getSettings();
settings.setLoadWithOverviewMode(true);
settings.setUseWideViewPort(true);
loadUrl(launchUrl);
}
关键是调用设置“LoadWithOverviewMode”和“UseWideViewPort”为真。
现在应用程序按预期工作
关于android - 在 cordova 7 上忽略设置视口(viewport)宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44923541/