简体   繁体   English

为什么1dp在Android上同时表示3px和2px?

[英]Why does 1dp mean 3px and 2px at the same time on Android?

I use the following style for GridView : 我对GridView使用以下样式:

<style name="CaldroidDefaultGridView">
    <item name="android:adjustViewBounds">true</item>
    <item name="android:gravity">center_horizontal</item>
    <item name="android:listSelector">@null</item>
    <item name="android:background">@color/caldroid_lighter_gray</item>
    <item name="android:horizontalSpacing">1dp</item>
    <item name="android:padding">1dp</item>
    <item name="android:verticalSpacing">1dp</item>

And it shows 3px for padding and 2px for spacing on Pixel 2 emulator. 并且在Pixel 2模拟器上显示3px用于填充,2px用于间隔。 If I implicitly specify 2px both for padding and spacing, then it shows 2px - so there is no extra pixel anywhere. 如果我隐式地指定2px用于填充和间距,那么它将显示2px-因此在任何地方都没有多余的像素。

It happens in both directions - horizontal and vertical. 它发生在水平和垂直两个方向。 For padding 1dp always means 3px. 对于填充1dp始终表示3px。 For spacing 1dp always means 2px. 对于间距1dp始终表示2px。

Why does 1dp mean 3px and 2px at the same time on Android? 为什么1dp在Android上同时表示3px和2px? How can I get the actual values then? 那我怎么得到实际值呢? Because I need to know them. 因为我需要了解他们。

PS I understand that it's hard to believe in the stated facts, so I can provide screenshots as the proof. 附言:我知道很难相信所陈述的事实,因此我可以提供屏幕截图作为证明。

dp means density-independent pixels. dp表示与密度无关的像素。 Every smartphone has a different number that goes around 300dp - 420dpi if I don't remember bad. 每个智能手机都有一个不同的数字,大约300dp-420dpi(如果我不记得不好的话)。 Anyway, your smartphone should have 1920x1080 as screen. 无论如何,您的智能手机的屏幕应该为1920x1080。 So that's because 1dp could be 2-3px at the same time. 这是因为1dp可能同时为2-3px。

You can read more in the official developer android website. 您可以在Android开发者官方网站上阅读更多内容。

https://developer.android.com/training/multiscreen/screendensities https://developer.android.com/training/multiscreen/screendensities

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

相关问题 android中px和dp之间的不一致 - Inconsistency between px and dp in android Android对话框,删除细灰色边框(〜2px)环绕对话框 - Android dialog, removing the thin grey border (~2px) sorrounding dialog android:scaleType =“ center”不能满足我的要求:dp vs px如何工作? - android:scaleType=“center” doesn't do what I think it should: how does dp vs px work? 如何以每秒2/3像素的速度移动对象(绘画) - How to move an object at 2/3px per second(paint) “ android:padding = -5dp”是什么意思? - what does “android:padding=-5dp” mean? 有没有办法获得 MP Android BarChart 的 px/dp 的条宽? - Is there a way I can get the Bar Width in px/dp's of an MP Android BarChart? 在 Android 应用程序中,我将所有 px 更改为 dp 和 sp。 现在打不开了。 怎么了? - In Android app,I changed all px to dp and sp. Now it is not opening. What is wrong? 如何确定Java中View的像素单位(px,dp等)? - How to determine the Pixel Unit (px, dp, etc) of a View in Java? 在 Kotlin 中将 Dp 转换为 Px - 这种转换永远不会成功 - Convert Dp to Px in Kotlin - This cast can never succeed 如何为不同的分辨率强制字体大小以px为单位 - How to force fonts to be same size in px for different resolutions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM