简体   繁体   English

相同的DPI但不同的英寸尺寸

[英]Same DPI but different inch size

I know the Internet is overwhelmed with questions about DPI px inches and so on. 我知道互联网上有关于DPI px英寸等问题的不堪重负。 But after several hours of googling my situation doesnt seem to happen to anyone else! 但经过几个小时的谷歌搜索后,我的情况似乎不会发生在其他任何人身上!

I have 2 devices custom build with android studio which are both mdpi. 我有两个设备自定义构建与android工作室,都是mdpi。 BUT one device is 3.65inch and the other device is an 10.1 inch. 但是一个设备是3.65英寸而另一个设备是10.1英寸。

I have created a folder with 2 images 250x125 with the dpi set to 160 dpi 我创建了一个文件夹,其中包含2张250x125图像,dpi设置为160 dpi

If normally I would declare my 2 images in my XML with dp units instead of pixels...I would suppose on both screens the result should be the same right ? 如果通常我会用dp单位而不是像素在我的XML中声明我的2个图像...我想在两个屏幕上结果应该是相同的吗?

Well it seems the images keep remaining the same size and don't look @ how many inch the device is 好吧,似乎图像保持相同的大小,不看@设备是多少英寸

So to set things clear: What do I have to change at my resources or my code so that my layout scales identical for different Inch sizes ? 所以要明确说明:我需要在资源或代码中更改哪些内容,以便我的布局在不同的英制尺寸下进行相同的缩放?

This is my GOOD layout for my mdpi 10.1 tablet : 这是我的mdpi 10.1平板电脑的GOOD布局: 在此输入图像描述

This is my BAD layout for my mdpi 3.65 device 这是我的mdpi 3.65设备的BAD布局 在此输入图像描述

How can I make it so that even on the 3.65 inch screen the buttons will scale to the same PROPORTIONS as the 10.1. 如何制作它,即使在3.65英寸的屏幕上,按钮也会缩放到与10.1相同的比例。 Not the inches...not the pixels...the proportions.... 不是英寸......不是像素......比例......

This is my XML File 这是我的XML文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:gravity="center">

    <Button
        android:id="@+id/buttonEnglish"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/english"
        android:layout_marginBottom="5sp"
        android:layout_marginLeft="5sp"
        android:layout_marginRight="2sp"
        android:layout_marginTop="0sp" />
    <Button
        android:id="@+id/buttonNederlands"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/nederlands"
        android:layout_marginBottom="5sp"
        android:layout_marginLeft="20sp"
        android:layout_marginRight="5sp"
        android:layout_marginTop="0sp"
        />
</LinearLayout>

I'm desperate... Thanx in advance 我很绝望......坦克提前

There is no need of Designing two xml layout. 不需要设计两个xml布局。

You can use Dimension for margin and padding according to device. 您可以根据设备使用Dimension作为边距和填充。

You are giving static value for margin. 您正在为保证金提供静态值。

Use dimen.xml in value folder each device. 在每个设备的值文件夹中使用dimen.xml。

Following code in your layout.xml will work for you. layout.xml中的以下代码将适合您。

android:layout_marginLeft="@dimen/margin_button"

Value folder name for dimen.xml: dimen.xml的值文件夹名称:

values-mdpi 价值观MDPI
values-hdpi 值,华电国际
values-xhdpi 价值观xhdpi
values-xxhdpi 价值观xxhdpi
values-sw600dp 价值观sw600dp

create dimen.xml in each values folder. 在每个值文件夹中创建dimen.xml。

and in dimen.xml you have to define value for margin in all values folder but value of that property is different according to device size like this: 在dimen.xml中,你必须在所有值文件夹中定义margin的值,但该属性的值根据设备大小而不同,如下所示:

values-mdpi 价值观MDPI

<dimen name="margin_button">20dp</dimen>

values-hdpi 值,华电国际

<dimen name="margin_button">23dp</dimen>

like wise in all values folders. 在所有值文件夹中都是明智的。

This might help explain the problem you are facing... 这可能有助于解释您所面临的问题......

You have an image that is 250x125 - that is 250 pixels wide by 125 pixels in height. 您的图像为250x125 - 宽度为250像素,高度为125像素。

You have specified 160 dpi - which means that 1 inch = 160 pixels. 您指定了160 dpi - 这意味着1英寸= 160像素。

So, both screens are doing what you ask and displaying the 250 pixels across 1.5625 inches. 因此,两个屏幕都按照您的要求进行操作,并在1.5625英寸处显示250像素。 On the large screen it looks "proportionally" correct. 在大屏幕上,它看起来“按比例”正确。 On the 3.65" screen the button takes up more than half the screen - just like you asked it to. 在3.65英寸的屏幕上,按钮占据屏幕的一半以上 - 就像你要求它一样。

If you want the smaller screen to look like the larger screen, then you have three options: 如果您希望较小的屏幕看起来像较大的屏幕,那么您有三个选项:

  1. adjust the size of the image and provide 2 image assets (or more for a wider variety of screens). 调整图像大小并提供2个图像资源(或更多用于更多种类的屏幕)。 This is why you can have resource folders for mdpi, hdpi, xhdpi, etc. You adjust the pixels in the image to accommodate the screen size. 这就是为什么你可以拥有mdpi,hdpi,xhdpi等资源文件夹的原因。你可以调整图像中的像素以适应屏幕大小。

  2. You use a "weighted" LinearLayout that adjusts the size of the space provided based on the available screen space. 您使用“加权” LinearLayout ,根据可用的屏幕空间调整提供的空间大小。 For this type of layout you should not worry about performance. 对于这种类型的布局,您不必担心性能。

  3. Do runtime scaling of the image based on screen size - use DisplayMetrics to get the size and density of the screen and adjust your image to fit the screen appropriately. 根据屏幕大小对图像进行运行时缩放 - 使用DisplayMetrics获取屏幕的大小和密度,并调整图像以适合屏幕。

The last option is the most flexible in many ways, because if you end up with a screen that is either very large or very small, you can make adjustments to do things like move buttons or text to another place on the screen (above or below other content). 最后一个选项在很多方面都是最灵活的,因为如果您最终得到的屏幕非常大或非常小,您可以进行调整,例如将按钮或文本移动到屏幕上的其他位置(上方或下方)其他内容)。 But for your specific problem, any of them will suffice. 但是对于你的具体问题,任何一个都足够了。

Thanx everyone for the answers. 谢谢大家的答案。 Due to answer from @Iacs I discovered that I had to made changes to my folder structure. 由于来自@Iacs的回答,我发现我必须对我的文件夹结构进行更改。

I have completely overlooked the fact that in the /res folder there can be more directories then just the standard "layout" directory. 我完全忽略了这样一个事实:在/ res文件夹中可以有更多目录,然后只有标准的“layout”目录。 You can create other directories with these names : layout-large, layout-xlarge, layout-small, and so on... In these folders you can paste your layout.xml and adjust the values... 您可以使用以下名称创建其他目录:layout-large,layout-xlarge,layout-small等等...在这些文件夹中,您可以粘贴layout.xml并调整值...

This is how things look now in my android studio note the layout folder structure: 这就是我的android工作室中现在的样子注意布局文件夹结构: 在此输入图像描述

And now ofcourse my 2 devices with both the same DPI but different screen size are showing my buttons the way I want them to be showned! 而现在我的2台具有相同DPI但不同屏幕尺寸的设备正按照我希望它们展示的方式显示我的按钮!

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

相关问题 计算器应用在同一屏幕尺寸上但DPI不同(420 DPI与480 DPI)的布局不同 - Calculator app different layout on same screen size but different DPI (420 DPI vs 480 DPI) 从DPI(每英寸点数)android获取图像大小(以像素为单位) - Get image size in pixels from DPI (dot per inch) android 我如何支持相同的屏幕尺寸但不同的dpi? - How do I support same screen size but different dpi? 如何解决不同屏幕尺寸的相同dpi问题 - how to solve same dpi problem with different screen size Android-相同dpi但屏幕尺寸不同的动态文本缩放 - Android - Dynamic text scaling for same dpi but different screen size 如何设置反应本机元素的尺寸以在不同的设备英寸尺寸中显示相同的尺寸 - How to set dimension of react native elements for showing same size in different device inch size 在Android中分辨率为1208 * 800像素,密度为240 dpi,屏幕尺寸为8英寸的布局res文件夹将是什么 - What will be layout res folder for Resolution 1208*800 px, Density 240 dpi, screen size 8 inch in Android Android 不同的屏幕尺寸/dpi,工作量更少 - Android different screen size/dpi with less work 如何计算不同设备dpi的图像尺寸 - How to calculate image size for different devices dpi Flutter 中的不同屏幕尺寸和 dpi 缩放比例 - Different Screen size and dpi scaling in Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM