Question

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.

Était-ce utile?

La solution

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;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top