一个LinearLayout中的边距,编程方式与DP [英] Margins of a LinearLayout, programmatically with dp
本文介绍了一个LinearLayout中的边距,编程方式与DP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有可能设置的LinearLayout的边缘,编程而与像素,但DP?
Is it possible to set the Margins of a LinearLayout, programmatically but not with pixels, but dp?
推荐答案
您可以使用<一个href="http://developer.android.com/intl/de/reference/android/util/DisplayMetrics.html">DisplayMetrics并确定屏幕像素密度。事情是这样的:
You can use DisplayMetrics and determine the screen density. Something like this:
int dpValue = 5; // margin in dips
float d = context.getResources().getDisplayMetrics().density;
int margin = (int)(dpValue * d); // margin in pixels
我记得最好使用地板的偏移量和取整的宽度。
As I remember it's better to use flooring for offsets and rounding for widths.
这篇关于一个LinearLayout中的边距,编程方式与DP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文