简体   繁体   English

适用于不同屏幕尺寸的Android CSS

[英]Android CSS for different screen sizes

I have a simple web page that displays some text and images and trying to find a non complicated way to handle multiple screens. 我有一个简单的网页,其中显示了一些文本和图像,并试图找到一种处理多个屏幕的简单方法。

I have looked through the docs and the only way I can see to do it right now is having to create three separate css files, one for low, one for medium, and one for high res. 我已经浏览了文档 ,现在唯一可以看到的方法是必须创建三个单独的CSS文件,一个为低档,一个为中档,一个为高分辨率。 Unfortunately each one of these have to have different font sizes and dimensions of every element/image. 不幸的是,每个元素/图像必须具有不同的字体大小和尺寸。

This seems rather wasteful considering every hdpi element should just be exactly 1.5x the size of the normal one. 考虑到每个hdpi元素的大小应该恰好是普通元素的1.5倍,这似乎很浪费。 Is there not a way to make Android just automatically scale this for us while using hdpi versions of the images instead of mdpi ones? 有没有办法让Android在使用hdpi版本的图像而不是mdpi版本的图像时自动为我们缩放比例?

Am I missing something here? 我在这里想念什么吗?

You can try using Media Queries in your stylesheet. 您可以尝试在样式表中使用媒体查询

Basically you say if viewport is smaller than X amount then initiate (or make changes to) this style or if viewport is larger then Y amount, implement (or update) this style. 基本上,您说的是,如果视口小于X数量,则启动(或更改)此样式;如果视口大于Y数量,则实施(或更新)此样式。 You can have as many possibilities as you wish and there is no need for extra styles because the change is implemented on the particular class or ID you choose once a certain width is reached. 您可以根据需要选择多种可能性,并且不需要其他样式,因为更改是在达到特定宽度后在您选择的特定类或ID上实现的。

Check out this article on Responsive Web Design 查看有关响应式Web设计的本文

Hope this is the sort of info you are after? 希望这是您所需要的信息?

I just looked on the very same issue. 我只是看了同样的问题。 Well, it seems that Android doesn't automatically adjust the content of Webview (the image, the font size, etc) according to the screen size like what it does automatically with the app icon. 好吧,似乎Android不会像屏幕上自动使用应用程序图标那样根据屏幕尺寸自动调整Webview的内容(图像,字体大小等)。

But, regarding the icon, actually, it's not automatically adjusted as well. 但是,实际上,图标也不是自动调整的。 You still have to provide the icon in three different sizes to accommodate the ldpi, mdpi, and hdpi resolution. 您仍然必须提供三种不同大小的图标以适应ldpi,mdpi和hdpi分辨率。

That's why we need to provide three different css files. 这就是为什么我们需要提供三个不同的CSS文件。 But, actually, if you read the doc you mentioned previously, you can write those three different css in one file, like this: 但是,实际上,如果您阅读了前面提到的文档,则可以在一个文件中编写这三个不同的CSS,如下所示:

header {
    background:url(medium-density-image.png);
}

@media screen and (-webkit-device-pixel-ratio: 1.5) {
    /* CSS for high-density screens */
    #header {
        background:url(high-density-image.png);
    }
}

@media screen and (-webkit-device-pixel-ratio: 0.75) {
    /* CSS for low-density screens */
    #header {
        background:url(low-density-image.png);
    }
}

The upper part is for the mdpi, the middle one for the hdpi, and the lower part is for the ldpi. 上部用于mdpi,中间用于hdpi,下部用于ldpi。 So, you can easily manage one file for those three different screen resolutions. 因此,您可以轻松地为这三种不同的屏幕分辨率管理一个文件。

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

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