以编程方式将LayoutParams上的高度设置为与密度无关的像素 [英] Programmatically set height on LayoutParams as density-independent pixels

查看:90
本文介绍了以编程方式将LayoutParams上的高度设置为与密度无关的像素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过任何方式设置 LayoutParams 作为与密度无关的像素(dp)?以编程方式设置的高度/宽度看起来是像素而不是dp.

Is there any way to set the height/width of a LayoutParams as density-independent pixels (dp)? It looks like the height/width, when set programmatically, are in pixels and not dp.

推荐答案

您需要将dip值转换为像素:

You need to convert your dip value into pixels:

int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, <HEIGHT>, getResources().getDisplayMetrics());

对我来说,这就是窍门.

For me this does the trick.

这篇关于以编程方式将LayoutParams上的高度设置为与密度无关的像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆