简体   繁体   English

android中px和dp之间的不一致

[英]Inconsistency between px and dp in android

OK, so here's a wobbly one... Before asking my question, I'll point out that I have read this: What is the difference between "px", "dp", "dip" and "sp" on Android? 好的,这是一个不稳定的问题。在问我的问题之前,我要指出我已经读过: Android上的“ px”,“ dp”,“ dip”和“ sp”什么区别? So here's my problem, I have some squares on screen (EditText) which I define in my XML using dp units, and identifying them square1, square2 etc., and they are positioned in a FrameLayout (since absolute layout is deprecated and that I don't see any other layout that would suit my purposes; I want to control the position of the squares on screen by programming, soI use 所以这是我的问题,我在屏幕上有一些正方形(EditText),它们使用dp单元在XML中定义,并标识出square1,square2等,并将它们放置在FrameLayout中(因为不赞成使用绝对布局,而我没有看到其他适合我目的的布局;我想通过编程来控制屏幕上正方形的位置,因此我使用

square1.setX(cc1x); 
square1.setY(cc1y);
square2.setX(cc2x); 
square2.setY(cc2y);

and so on, and where cc1x, cc1y, cc2x, cc2y are int variables that change according to the logic of the app. 依此类推,其中cc1x,cc1y,cc2x,cc2y是根据应用程序逻辑更改的int变量。 The problem is that the java syntax doesn't allow adding a unit to this integer value of the variables, so on some screens my app works just perfectly, but on screens with a different density, everything is either too far apart, either overlapping... 问题在于Java语法不允许在变量的此整数值中添加单位,因此在某些屏幕上我的应用程序运行得很好,但是在密度不同的屏幕上,所有内容之间的距离太远,或者重叠。 ..

And finally the question(s): Is there a way of "forcing" the units into the setX / setY statements? 最后是一个或多个问题:是否可以将单元“强制”到setX / setY语句中?

or 要么

Is there a way to get the density of the screen of the device where my app will run? 有没有一种方法可以获取运行我的应用程序的设备屏幕的密度? (in which case I could recalculate the X/Y coordinates accordingly) (I didn't do research on this second possibility yet, as I just thought of it whilst writing this, so please accept my apologies if it is a duplicate of some other question). (在这种情况下,我可以相应地重新计算X / Y坐标)(我还没有对第二种可能性进行研究,因为我在撰写本文时只是想到了这种情况,因此,如果它与其他某种重复出现,请接受我的道歉题)。 Thanks in advance for your thoughts. 预先感谢您的想法。

You can add this method to easily convert between DP on the screen and PX of the device 您可以添加此方法以在屏幕上的DP和设备的PX之间轻松转换

 public static float convertDpToPx(Context context, int dp) {
    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
}

I wanted to update on this thread, since I've found a far better solution to the problem, though the answers you guys (& girls?) really did teach me a lot, for which my gratitude. 我想对此线程进行更新,因为我找到了一个更好的解决方案,尽管你们(和女孩?)的答案确实确实教会了我很多,对此我深表感谢。 Finally, njzk2's answer has made me reconsider the whole approach to what I wanted to do. 最后,njzk2的答案使我重新考虑了要执行的整个方法。 So, instead of moving my boxes around, which resulted in the dimensioning problems, now I just move the content of the boxes around, leaving the boxes nicely positioned in place, which is by far more easy. 因此,现在不用移动我的盒子,而这会导致尺寸问题,现在我只需移动盒子的内容,将盒子放置在适当的位置即可,这要容易得多。 Anyway, I hope this will result usefull to other starters like me. 无论如何,我希望这将对其他像我这样的初学者有用。

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

相关问题 为什么1dp在Android上同时表示3px和2px? - Why does 1dp mean 3px and 2px at the same time on Android? Android Studio和WebStorm之间的语言级别不一致 - Language level inconsistency between Android Studio and WebStorm 有没有办法获得 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:scaleType =“ center”不能满足我的要求:dp vs px如何工作? - android:scaleType=“center” doesn't do what I think it should: how does dp vs px work? 在 Android 应用程序中,我将所有 px 更改为 dp 和 sp。 现在打不开了。 怎么了? - In Android app,I changed all px to dp and sp. Now it is not opening. What is wrong? Android:以英寸为单位设置视图宽度(在英寸和dp之间转换) - Android: Setting width of View in inches (converting between inches and dp) Android布局不一致 - Android layout inconsistency 如何确定Java中View的像素单位(px,dp等)? - How to determine the Pixel Unit (px, dp, etc) of a View in Java? android.support.v4.app和android.app之间的不一致错误 - Inconsistency errors between android.support.v4.app and android.app 在 Kotlin 中将 Dp 转换为 Px - 这种转换永远不会成功 - Convert Dp to Px in Kotlin - This cast can never succeed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM