Does setWidth(int pixels) use dip or px?

前端 未结 6 1389
执念已碎
执念已碎 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:41

    Method setWidth(100), set 100 px as width(not in dp).So you may face width varying problems on different android phones.So use measurement in dp instead of pixels.Use the below code to get measurement in dp of sample width=300px and height=400px.

    int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 300, getResources().getDisplayMetrics());
    
    int Height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 400, getResources().getDisplayMetrics());
    

提交回复
热议问题