简体   繁体   English

针对不同的Android屏幕尺寸/密度扩展Phonegap应用?

[英]Scaling a Phonegap app for different Android screen sizes/densities?

I have a Phonegap app designed to run on Android phones and tablets. 我有一个Phonegap应用程序,旨在在Android手机和平板电脑上运行。 The scale of text and images looks fine on a phone, but too small on a 7” tablet. 文字和图像的比例在手机上看起来很好,但在7英寸平板电脑上却太小了。

Is there a way to set the scale for different screen sizes/densities that works for a Phonegap-based app? 有没有办法设置适用于基于Phonegap的应用程序的不同屏幕尺寸/密度的比例? For a native Android app, multiple screen layouts (as mentioned in the android docs ) would be a solution, but can this be used for a Phonegap-based app? 对于原生的Android应用程序,多个屏幕布局(如Android文档中所述 )将是一个解决方案,但这可以用于基于Phonegap的应用程序吗?

I could use CSS media queries to set font sizes based on screen size, but I want to scale the entire interface (including images) proportionally. 我可以使用CSS媒体查询根据屏幕大小设置字体大小,但我想按比例缩放整个界面(包括图像)。

I tried using the CSS zoom property along with media queries to target specific screen sizes, but this screws up absolute positioning and interferes with the function of UI elements such as iScroll: 我尝试使用CSS zoom属性以及媒体查询来定位特定的屏幕尺寸,但这会破坏绝对定位并干扰UI元素的功能,例如iScroll:

@media only screen and (min-device-width : 768px) {
    body{
        zoom: 125%;
    }   
}
@media only screen and (min-device-width : 1024px){ 
    body{
        zoom: 150%;
    }
}

I also tried using the targetdensity-dpi meta viewport property - adjusting it to 120dpi makes the scale better on the 7” tablet, but too large on the phone. 我还尝试使用targetdensity-dpi元视口属性 - 将其调整为120dpi使得7“平板电脑上的刻度更好,但在手机上太大了。 Since this is hard-coded in HTML rather than CSS, media queries can't be used to vary it based on screen size: 由于这是在HTML而不是CSS中进行硬编码,因此不能使用媒体查询根据屏幕大小来改变它:

<meta name="viewport" 
   content="width=device-width, initial-scale=1, targetdensity-dpi=120dpi">

Here's some screenshots from a testcase Phonegap app: 以下是来自测试用Phonegap应用程序的一些截图:

Before we give up on phonegap lets try improve it. 在我们放弃phonegap之前,请尝试改进它。 I was stuck until I solved it using this solution. 我被困住了,直到我用这个解决方案解决了它。

Change your viewport to this : 将视口更改为:

<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, target-densitydpi=medium-dpi, user-scalable=0" />

Ref : Phonegap Application text and layout too small 参考: Phonegap应用程序文本和布局太小

Just noticed I never answered this. 刚注意到我从未回答过这个问题 The solution I used in the end was to use media queries to explicitly set font sizes and image assets based on the device size. 我最终使用的解决方案是使用媒体查询根据设备大小显式设置字体大小和图像资源。 Device testing showed this worked on all my target devices: iPhone, iPad, Android phones, Android 7" tablets, Android 10" tables. 设备测试显示这适用于我的所有目标设备:iPhone,iPad,Android手机,Android 7“平板电脑,Android 10”表。 Hope it helps someone else. 希望它可以帮助别人。

For example: 例如:

/* Start with default styles for phone-sized devices */
    p,li{
        font-size: 0.9em;
    }
    h1{
        font-size: 1.2em;
    }
    button{
        width: 24px;
        height: 12px;
    }
    button.nav{
        background-image: url('img/phone/nav.png');
    }

@media only screen and (min-device-width : 768px) { /* 7" tablets */
    p, li{
        font-size: 1.3em !important;
    }
    h1{
        font-size: 1.6em !important;
    }
    button{
          width: 32px;
          height: 16px;
    }
    button.nav{
        background-image: url('img/tablet7/nav.png');
    }
}
@media only screen and (min-device-width : 1024px) { /* 10" tablets and iPad */
    p, li{
        font-size: 1.5em !important;
    }
    h1{
        font-size: 1.8em !important;
    }
    button{
        width: 48px;
        height: 24px;
    }
    button.nav{
        background-image: url('img/tablet10/nav.png');
    }
}

根据Pete LePage( https://plus.google.com/+GoogleChromeDevelopers/posts/BKHdSgQVFBB ),我们认为target-densitydpi已被弃用并从Webkit中删除,我认为更好的方法是使用媒体查询来定位高dpi丢弃phonegap之前的设备

I have a couple of different solutions to suggest: 我有几个不同的解决方案建议:

  1. Change viewport dynamically using Javascript ( more info here ) 使用Javascript动态更改视口( 此处有更多信息
  2. Set all sizes in rem units rather than px . rem单位而不是px设置所有大小。 Then you can scale everything by adjusting document font-size. 然后,您可以通过调整文档字体大小来扩展所有内容。 Sketching an example: 草绘一个例子:

     function resize() { var w = document.documentElement.clientWidth; var h = document.documentElement.clientHeight; var styleSheet = document.styleSheets[0]; // ar = aspect ratio h/w; Replace this with your apps aspect ratio var ar = 1.17; // x = scaling factor var x = 0.1; var rem; if (h / w > ar) { // higher than aspect ratio rem = x * w; } else { // wider than aspect ratio rem = x * h; } document.documentElement.style.fontSize = rem + 'px'; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM