在程序中如何以dp为单位设置控件的宽和高

来源:互联网 发布:沈晓海花姑子知乎 编辑:程序博客网 时间:2024/06/06 02:19

 问:

I'm doing:

button.setLayoutParams(new GridView.LayoutParams(65, 65));

According to the docs the units for the width and height (both 65 in the above) are "pixels". How do you force this to be device independent pixels, or "dp"?

 

回答:

You'll have to convert it from dps to pixels using the display scale factor.

final float scale = getContext().getResources().getDisplayMetrics().density; int pixels = (int) (dps * scale + 0.5f); 

 

原创粉丝点击