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

后端 未结 3 1252
长情又很酷
长情又很酷 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 15:43

    Solved here.

    Extract:

    DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
    float dp = 20f;
    float fpixels = metrics.density * dp;
    int pixels = (int) (fpixels + 0.5f);
    

提交回复
热议问题