简体   繁体   English

此示例的最小布局宽度如何工作?

[英]How do smallest width for layouts work with this example?

I have read the officel documentation but still dosn't understand it! 我已经阅读了officel文档,但仍然不理解!

I'm playing and experimenting with the smallest-width layouts in Android and it dosn't give any sense to me. 我正在玩Android上最小宽度的布局并在进行实验,这对我没有任何意义。

I have created these layouts: sw-300dp/350dp/360dp/400dp/450dp/500dp/ But my Galaxy S7 use the sw-360dp layout . 我已经创建了以下布局: sw-300dp/350dp/360dp/400dp/450dp/500dp/但是我的Galaxy S7使用sw-360dp layout But 360dp is far from any of the numbers provedid by getDisplayMetrics(); 但是360dp远没有getDisplayMetrics();证明的任何数字getDisplayMetrics(); or the numbers of the screen I got from GSMArena. 或我从GSMArena获得的屏幕编号。 So why do Galaxy S7 use 360dp and not 500dp since the xdpi is 580dp and closer to 500dp? 那么,为什么银河S7使用360dp ,而不是500dp因为xdpi是580dp,更贴近500dp?

Test Phone: Samsung Galaxy S7 测试电话:三星Galaxy S7

Screen Size: 5.1" 屏幕尺寸: 5.1英寸

Resolution: 1440x 2560 (577 ppi) 分辨率: 1440x 2560(577 ppi)

From det Logcat I retrived the following screen values of the phone: 从det Logcat,我检索了电话的以下屏幕值:

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

The density of the screen on the S7 is 4.0 S7上的屏幕密度为4.0

That means that there are 4 pixels per dp, therefore the screen is 1440 / 4 = 360dp wide. 这意味着每dp有4个像素,因此屏幕为1440/4 = 360dp宽。

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);
}

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

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