Does setWidth(int pixels) use dip or px?

前端 未结 6 1398
执念已碎
执念已碎 2020-11-28 17:58

Does setWidth(int pixels) use device independent pixel or physical pixel as unit? For example, does setWidth(100) set the a view\'s width to 100 dips or 100 pxs?

Tha

6条回答
  •  星月不相逢
    2020-11-28 18:25

    The correct way to obtain a constant number of DIPs in code is to create a resources XML file containing dp values a bit like:

    
    
        100dp
        75dp
    
    

    Then refer to the resource in your code like so:

    float width = getResources().getDimension(R.dimen.image_width));
    float height = getResources().getDimension(R.dimen.image_height));
    

    The float you have returned will be scaled accordingly for the pixel density of the device and so you don't need to keep replicating a conversion method throughout your application.

提交回复
热议问题