Android 从 Java 代码指定 DIP 维度的正确方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2238883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the correct way to specify dimensions in DIP from Java code?
提问by Pavel Lahoda
I found that it is possible to set dimensions of my interface elements in XML layouts using DIPs as in following fragment:
我发现可以使用 DIP 在 XML 布局中设置我的界面元素的尺寸,如下面的片段所示:
android:layout_width="10dip"
But all Java interface takes integer as arguments and there is no way to specify dimensions in DIPs. What is the correct way to calculate this?
但是所有 Java 接口都将整数作为参数,并且无法在 DIP 中指定维度。计算这个的正确方法是什么?
I figured that I have to use property density of DisplayMetrics
class but is this a correct way?
我想我必须使用DisplayMetrics
类的属性密度,但这是正确的方法吗?
May I rely on this formula being always correct?
我可以相信这个公式总是正确的吗?
pixels * DisplayMetrics.density = dip
像素 * DisplayMetrics.密度 = 倾角
Is there a utility function for the conversion somewhere in Android?
Android 中的某处是否有用于转换的实用程序函数?
回答by sirhc
There is an existing utility method built called TypedValue.applyDimensions(int, float, DisplayMetrics)
that does this.
有一个名为的现有实用程序方法TypedValue.applyDimensions(int, float, DisplayMetrics)
可以执行此操作。
Here's how to use it:
以下是如何使用它:
// returns the number of pixels for 123.4dip
int value = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
(float) 123.4, getResources().getDisplayMetrics());
There's a list of other types that you can use with it including COMPLEX_UNIT_SP
, COMPLEX_UNIT_PT
all found in the page I linked above. If you exclude the (int) typecast, you'll get the floating point number.
有一个其他类型的列表,您可以使用它,包括COMPLEX_UNIT_SP
, COMPLEX_UNIT_PT
所有这些都可以在我上面链接的页面中找到。如果排除 (int) 类型转换,您将获得浮点数。
I encountered the same problem and found this through poking around the code.
我遇到了同样的问题,并通过查看代码发现了这一点。
回答by Roman Nurik
That is the correct formula there. Although DisplayMetrics.density
is not a static member, so, according to the same document you alluded to, correct usage is:
那是那里的正确公式。虽然DisplayMetrics.density
不是静态成员,因此,根据您提到的同一文档,正确的用法是:
// Maybe store this in a static field?
final float SCALE = getContext().getResources().getDisplayMetrics().density;
// Convert dips to pixels
float valueDips = 16.0f;
int valuePixels = (int)(valueDips * SCALE + 0.5f); // 0.5f for rounding
回答by Matt Stoker
Using the display metrics density field, you can get a scaling factor to convert to/from dips. Use the Math.round method to convert from a float to an int without needing a cast or adding 0.5
使用显示指标密度字段,您可以获得一个比例因子来转换为/从下降。使用 Math.round 方法将 float 转换为 int 无需强制转换或添加 0.5
// Converting dips to pixels
float dips = 20.0f;
float scale = getContext().getResources().getDisplayMetrics().density;
int pixels = Math.round(dips * scale);
// Converting pixels to dips
int pixels = 15;
float scale = getContext().getResources().getDisplayMetrics().density;
float dips = pixels / scale;
回答by ercan
I think the best way is not to define any dimensions in code, but use the values/dimens.xml
file instead. You can always define your dimension in the desired unit like:
我认为最好的方法不是在代码中定义任何维度,而是使用values/dimens.xml
文件。您始终可以使用所需的单位定义尺寸,例如:
<dimen name="my_layout_height">120dp</dimen>
and then refer to this in your Activity like:
然后在您的 Activity 中引用它,例如:
getResources().getDimensions(R.dimen.my_layout_height);
This would return pixelsafter doing the necessary conversions. And also of course you can refer to this in your other XMLs like:
这将在进行必要的转换后返回像素。当然,您也可以在其他 XML 中引用它,例如:
android:layout_width="@dimen/my_layout_height"