Margins of a LinearLayout, programmatically with dp

后端 未结 6 1877
生来不讨喜
生来不讨喜 2020-12-01 01:12

Is it possible to set the Margins of a LinearLayout, programmatically but not with pixels, but dp?

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-01 01:58

    The following method works for me for converting pixels to dp:

    int pixelToDP(int pixel) {
        final float scale = RaditazApplication.getInstance().getResources().getDisplayMetrics().density;
        return (int) ((pixel * scale) + 0.5f);
    }
    

提交回复
热议问题