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

后端 未结 3 1376
长情又很酷
长情又很酷 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:58

    Just for completeness: Another solution (which I'd prefer) for the question is given here

    setTextSize(float) expects a scaled pixel value. So, setTextSize(10) would give you the desired result. However, getDimension() and getDimensionPixelSize() return the size in units of pixels.

    So for your example it would be

    setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.edTxt_text_size));
    

    where 10dp is set in your dimens.xml file for example.

提交回复
热议问题