문제

Hello I am setting padding to the ImageView and each device is showing me different padding, somewhere it is less or somewhere it is more.

I am doing this

int padding = Util.dpFromPx(6, this);
thumbnailImageView.setPadding(padding, padding, padding, padding);
thumbnailImageView.setBackgroundColor(Color.WHITE);

// convert the px to dp
public static int dpFromPx(float px, Context context) {
    return (int) (px / context.getResources().getDisplayMetrics().density);
}

I want to keep padding same to all device. Any idea to do that ?

Sony device

enter image description here

Samsung Dous

enter image description here

도움이 되었습니까?

해결책

SetPadding receive the values in pixels.

So you need to start from dips and convert to pixels:

int paddingInPx = Util.dipsToPixels(6);
thumbnailImageView.setPadding(paddingInPx , paddingInPx , paddingInPx , paddingInPx );

public int dipsToPixels(int dips)
    {
        return (dips * getApplicationContext().getResources().getDisplayMetrics().density + 0.5f);
    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top