问:
I'm doing:
button.setLayoutParams(new GridView.LayoutParams(65, 65));
According to the docs the units for the width and height (both 65 in the above) are "pixels". How do you force this to be device independent pixels, or "dp"?
回答:
You'll have to convert it from dps to pixels using the display scale factor.
final float scale = getContext().getResources().getDisplayMetrics().density;
int pixels = (int) (dps * scale + 0.5f);
本文解答了如何在Android中将布局参数从设备独立像素(dp)转换为像素单位的问题。通过使用显示比例因子,可以确保应用在不同分辨率的设备上正常显示。

被折叠的 条评论
为什么被折叠?



