问题描述
我想载入的价值,因为它是。我有两个 dimension.xml
文件,一个在 /res/values/dimension.xml
,另一个在 /res/values-sw360dp/dimension.xml
。
I'd like to load the value as it is.I have two dimension.xml
files, one in /res/values/dimension.xml
and the other one in /res/values-sw360dp/dimension.xml
.
从源头code我愿意做这样的事情
From source code I'd like to do something like
getResources().getDimension(R.dimen.tutorial_cross_marginTop);
这工作,但我得到的值乘以倍屏幕像素密度系数(1.5华电国际,2.0 xhdpi,等等)。
This works but the value I get is multiplied times the screen density factor (1.5 for hdpi, 2.0 for xhdpi, etc).
我也试着做
getResources().getString(R.dimen.tutorial_cross_marginTop);
这工作,但我得到了在蘸结尾的字符串...
This works but I get a string that ends in "dip"...
感谢您的帮助!
推荐答案
在我dimens.xml我有
In my dimens.xml I have
<dimen name="test">48dp</dimen>
在code。如果我这样做
In code If I do
int valueInPixels = (int) getResources().getDimension(R.dimen.test)
这将返回72为文档的状态(在我的情况48dp×1.5)乘以电流密度电话
this will return 72 which as docs state is multiplied by density of current phone (48dp x 1.5 in my case)
完全一样的文档状态:
检索的尺寸为特定资源ID。单位转换 是基于与所述资源相关联的当前DisplayMetrics
所以,如果你想确切的DP值,就像在XML只是把它与DisplayMetrics密度
so if you want exact dp value just as in xml just divide it with DisplayMetrics density
int dp = (int) (getResources().getDimension(R.dimen.test) / getResources().getDisplayMetrics().density)
DP将是现在的48
dp will be 48 now
这篇关于从水库/价值/ dimension.xml装载尺寸值从源头code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!