How do I set Layout and Text size to DP inside program?

后端 未结 3 1251
长情又很酷
长情又很酷 2021-01-31 15:02

Basically I have this inside XML, but I have to recreate it inside a code. How do I do it?



        
3条回答
  •  失恋的感觉
    2021-01-31 16:02

    You can use:

    float pixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics());
    

    Now the value of pixels is equivalent to 10dp at the device's current screen density.

    The TypedValue contains other similar methods that help in conversion.

提交回复
热议问题