Margins of a LinearLayout, programmatically with dp

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

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

6条回答
  •  Happy的楠姐
    2020-12-01 01:44

    You can convert dp to px, for example convert 5dp to px:

    Resources r = mContext.getResources();
    int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, r.getDisplayMetrics());
    

提交回复
热议问题