Programmatically set height on LayoutParams as density-independent pixels

前端 未结 5 2077
迷失自我
迷失自我 2020-12-07 08:24

Is there any way to set the height/width of a LayoutParams as density-independent pixels (dp)? It looks like the height/width, when set programmatically, are in pixels and n

5条回答
  •  孤街浪徒
    2020-12-07 08:49

    You need to convert your dip value into pixels:

    int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, , getResources().getDisplayMetrics());
    

    For me this does the trick.

提交回复
热议问题