简体   繁体   English

Android Studio布局dp

[英]Android Studio layout dp

In Android Studio when I make layout file and gives widths and heights in dp and change the device in preview the items shifts , but dp is density independent so screen width and height has to be 160dp for every screen, please correct me if i am wrong and if possible give me solution. 在Android Studio中,当我制作布局文件并在dp中提供宽度和高度并在预览中更改设备时,设备移位了,但是dp与密度无关,因此每个屏幕的屏幕宽度和高度都必须为160dp,如果我错了,请纠正我如果可能,请给我解决方案。 I am trying to make layout file which will look as it is on all devices. 我正在尝试使布局文件在所有设备上看起来都一样。 Thank you so much. 非常感谢。

Hello you aren't wrong when you say dp is density independent as you can see in the Material Design guidelines 您好,当您说dp与密度无关时,您并没有错,正如您在材料设计指南中所见

"Density independence" refers to the uniform display of UI elements on screens with different densities. “密度独立性”是指UI元素在具有不同密度的屏幕上的均匀显示。

Density-independent pixels (pronounced “dips”) are flexible units that scale to uniform dimensions on any screen. 与密度无关的像素(发音为“ dip”)是可以在任何屏幕上缩放为统一尺寸的灵活单位。 When developing an Android application, use dp to display elements uniformly on screens with different densities. 开发Android应用程序时,请使用dp在具有不同密度的屏幕上均匀显示元素。

But when you give specific values to margins and paddings you can never be completely sure if it'll always turn out the way you had planned it to. 但是,当您为边距和填充赋予特定的值时,您将永远无法完全确定它是否总是会按照您的计划进行。

To resolve this, we had to create multiple layout files for multiple screen densities and place them is seperate resource folders. 为了解决这个问题,我们必须为多个屏幕密度创建多个布局文件,并将它们放置在单独的资源文件夹中。 But with the introduction of ConstraintLayout you no more need to worry about that. 但是随着ConstraintLayout的引入,您不必再为此担心。

As you can read here, 如您在这里所读,

ConstraintLayout allows you to create large and complex layouts with a flat view hierarchy (no nested view groups). ConstraintLayout允许您使用平面视图层次结构(无嵌套视图组)创建大型而复杂的布局。 It's similar to RelativeLayout in that all views are layed out according to relationships between sibling views and the parent layout, but it's more flexible than RelativeLayout and easier to use with Android Studio's Layout Editor. 它与RelativeLayout相似,所有视图均根据同级视图和父级布局之间的关系进行布局,但它比RelativeLayout更加灵活,并且更易于与Android Studio的布局编辑器一起使用。

Read more about ConstraintLayout here 在此处阅读有关ConstraintLayout的更多信息

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

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