Avoid using px as units use dp instead

               

今天在写程序的时候,使用px设置组件宽度,弹出Avoid using "px" as units; use "dp" instead的提示,并且编译不通过

dp是可以根据屏幕大小变化的,而px就不行,主要是Android不主张再使用固定的长度单位了,如果想把dp转换成px值,

那么使用如下的代码

public static int dpToPixels(Context context, float dp) {    final float scale = context.getResources().getDisplayMetrics().density;    return (int) (dp * scale + 0.5f);}


           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

猜你喜欢

转载自blog.csdn.net/hffyyff/article/details/86489959