简体   繁体   中英

How do smallest width for layouts work with this example?

I have read the officel documentation but still dosn't understand it!

I'm playing and experimenting with the smallest-width layouts in Android and it dosn't give any sense to me.

I have created these layouts: sw-300dp/350dp/360dp/400dp/450dp/500dp/ But my Galaxy S7 use the sw-360dp layout . But 360dp is far from any of the numbers provedid by getDisplayMetrics(); or the numbers of the screen I got from GSMArena. So why do Galaxy S7 use 360dp and not 500dp since the xdpi is 580dp and closer to 500dp?

Test Phone: Samsung Galaxy S7

Screen Size: 5.1"

Resolution: 1440x 2560 (577 ppi)

From det Logcat I retrived the following screen values of the phone:

getDisplayMetrics().densityDpi: 640
getDisplayMetrics().xdpi: 580
getDisplayMetrics().ydpi: 575:
getDisplayMetrics().widthPixels: 1440

The density of the screen on the S7 is 4.0

That means that there are 4 pixels per dp, therefore the screen is 1440 / 4 = 360dp wide.

I use the following methods frequently:

public static int dpToPx(int dp) {
    return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
}

public static int pxToDp(int px) {
    return (int) (px / Resources.getSystem().getDisplayMetrics().density);
}

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