简体   繁体   English

Android:为不同大小的屏幕调整UI元素大小的正确方法-DP vs?

[英]Android: proper way to size UI elements for different size screens - dp vs?

Basic question here - if I define a widget in the following manner: 这里的基本问题-如果我以以下方式定义小部件:

<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@android:color/blue"/>

and I look at the widget on two different devices - one of them with a large screen and one of them with a small screen - which of the following will happen? 然后我查看了两种不同设备上的小部件-其中一台具有大屏幕,而一台具有小屏幕-将会发生以下哪项?

a) The widget will appear the same "size" on both screens - ie if it takes up one inch of real space on the big screen, it will take up one inch of real space on the small one, even if it that means that it doesn't all fit in the visible screen area. a)小部件在两个屏幕上都将显示相同的“大小”-即,如果它在大屏幕上占据一英寸的实际空间,则在小屏幕上将占据一英寸的实际空间,即使这意味着并非全部适合可见屏幕区域。

b) The widget will appear as one inch on the big screen, but as some fraction of an inch on the small screen. b)小部件在大屏幕上将显示为一英寸,而在小屏幕上将显示为一英寸的几分之一。 However the result will be relative - if the widget took up three quarters of the width of the large screen, it will take up exactly three quarters of the width of the small screen. 但是结果将是相对的-如果小部件占据了大屏幕宽度的四分之三,它将恰好占据了小屏幕宽度的四分之三。

Now, assuming that your answer was a - how do I redefine the widget to make it b? 现在,假设您的答案是-我如何重新定义小部件以使其成为b? Assuming your answer was b, how do I redefine the widget to make it a? 假设您的答案是b,如何将小部件重新定义为a? Many thanks. 非常感谢。

DP is "density independent pixel" which is by definiton equals 1px on a screen with a dpi (dot per inch) of 160. Every device has a different dpi, the cheaper ones usually have less, the more expensive ones more etc. DP是“密度无关像素”,按定义在屏幕上的dpi(每英寸点数)为160像素时等于1像素。每个设备的dpi都不相同,便宜的设备通常更少,而更昂贵的设备更多,等等。

You can calculate "real" size based on the device's specification but to answer your question, 50dp will look different on different screens, and their "real size" will be also different. 您可以根据设备的规格计算“实际”尺寸,但要回答您的问题,50dp在不同的屏幕上看起来会有所不同,并且它们的“实际尺寸”也会有所不同。

You can read more about this in the Android docs: 您可以在Android文档中阅读有关此内容的更多信息:

http://developer.android.com/guide/practices/screens_support.html http://developer.android.com/guide/practices/screens_support.html

Each widget must define a minWidth and minHeight, indicating the minimum amount of space it should consume by default....When your widget is added, it will be stretched to occupy the minimum number of cells, horizontally and vertically, required to satisfy its minWidth and minHeight constraints. 每个小部件都必须定义一个minWidth和minHeight,指示默认情况下它应该消耗的最小空间量。...添加小部件时,它将被拉伸以占据满足其最小水平和垂直方向所需的最小单元数minWidth和minHeight约束。

http://developer.android.com/guide/practices/ui_guidelines/widget_design.html http://developer.android.com/guide/practices/ui_guidelines/widget_design.html

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

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