문제

I want to set layout_marginTop to 800px using setLayoutParams(). But I want know what would be this value in dp unit?

Thanks in advance.

도움이 되었습니까?

해결책

The logical density of the display is given in the DisplayMetrics class, and can be retrieved with,

getResources().getDisplayMetrics().density

Thus, to convert dp to px, you would do,

int density = getResources().getDisplayMetrics().density;
int px = (int) (dp * density);

To convert px to dp, just perform the inverse operation,

int dp = px/density;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top