简体   繁体   中英

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. 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

"Density independence" refers to the uniform display of UI elements on screens with different densities.

Density-independent pixels (pronounced “dips”) are flexible units that scale to uniform dimensions on any screen. When developing an Android application, use dp to display elements uniformly on screens with different densities.

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.

As you can read here,

ConstraintLayout allows you to create large and complex layouts with a flat view hierarchy (no nested view groups). 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.

Read more about ConstraintLayout here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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