通过编程设置高度上的LayoutParams的密度无关的像素 [英] Programatically set height on LayoutParams as density-independent pixels
本文介绍了通过编程设置高度上的LayoutParams的密度无关的像素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有什么办法来设置的<一个高度/宽度href="http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html">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 programatically, are in pixels and not dp.
推荐答案
您需要将您畅游值转换为像素:
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屋!
查看全文