質問

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