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
.
我想按原样加载这个值。我有两个维度。xml文件,一个in /res/values/dimension。xml和/res/values-sw360dp/维度。xml。
From source code I'd like to do something like
从源代码我想做一些类似的事情
getResources().getDimension(R.dimen.tutorial_cross_marginTop);
This works but the value I get is multiplied times the screen density factor (1.5 for hdpi, 2.0 for xhdpi, etc).
这是有效的,但是我得到的值是乘以屏幕密度因子(hdpi的1.5,xhdpi的2.0等等)。
I also tried to do
我也试着去做
getResources().getString(R.dimen.tutorial_cross_marginTop);
This would work in principle but I get a string that ends in "dip"...
这在原则上行得通,但我得到了一个以“dip”结尾的字符串……
7 个解决方案
#1
629
In my dimens.xml I have
在我dimens。xml我有
<dimen name="test">48dp</dimen>
In code If I do
用代码
int valueInPixels = (int) getResources().getDimension(R.dimen.test)
this will return 72 which as docs state is multiplied by density of current phone (48dp x 1.5 in my case)
这将返回72作为文档状态乘以当前电话的密度(48dp x 1.5在我的例子中)
exactly as docs state :
如文件所述:
Retrieve a dimensional for a particular resource ID. Unit conversions are based on the current DisplayMetrics associated with the resources.
获取特定资源ID的维度。单元转换基于与资源关联的当前DisplayMetrics。
so if you want exact dp value just as in xml just divide it with DisplayMetrics density
如果你想要准确的dp值就像在xml中一样用DisplayMetrics密度来划分
int dp = (int) (getResources().getDimension(R.dimen.test) / getResources().getDisplayMetrics().density)
dp will be 48 now
dp现在是48。
#2
22
The Resource
class also has a method getDimensionPixelSize() which I think will fit your needs.
Resource类还有一个getdimensional pixelsize()方法,我认为它可以满足您的需要。
#3
17
Context.getResources().getDimension(int id);
#4
7
For those who just need to save some int
value in the resources, you can do the following.
对于那些只需要在资源中节省一些int值的人,您可以执行以下操作。
integers.xml
integers.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="default_value">100</integer>
</resources>
Code
代码
int defaultValue = getResources().getInteger(R.integer.default_value);
#5
2
You can write integer in xml file also..
have you seen [this] http://developer.android.com/guide/topics/resources/more-resources.html#Integer ? use as .
您也可以在xml文件中写入integer。你看过[this] http://developer.android.com/guide/topics/resources/more resources.html#Integer吗?使用。
context.getResources().getInteger(R.integer.height_pop);
#6
1
This works but the value I get is multiplied times the screen density factor
(1.5 for hdpi, 2.0 for xhdpi, etc).
I think it is good to get the value as per resolution but if you not want to do this give this in px.......
我认为按分辨率得到的价值是好的,但是如果你不想这样做,请用px来表示……
Density-independent pixel (dp)
密度独立像素(dp)
A virtual pixel unit that you should use when defining UI layout, to express layout dimensions or position in a density-independent way. The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, which is the baseline density assumed by the system for a "medium" density screen. At runtime, the system transparently handles any scaling of the dp units, as necessary, based on the actual density of the screen in use. The conversion of dp units to screen pixels is simple: px = dp * (dpi / 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels.
You should always use dp units when defining your application's UI, to ensure proper display of your UI on screens with different densities.
在定义UI布局时应该使用的虚拟像素单元,以一种与密度无关的方式表示布局维度或位置。与密度无关的像素相当于160 dpi屏幕上的一个物理像素,这是系统假定的“中等”密度屏幕的基线密度。在运行时,系统根据实际使用的屏幕密度,透明地处理dp单元的任何缩放。dp单位到屏幕像素的转换很简单:px = dp * (dpi / 160)。例如,在240 dpi屏幕上,1 dp等于1.5物理像素。在定义应用程序的UI时,应该始终使用dp单元,以确保在不同密度的屏幕上正确显示UI。
I think it is good to change the value as per resolution but if you not want to do this give this in px.......
我认为改变每个分辨率的值很好,但是如果你不想这样做的话,就在px里做这个吧。
refer this link
请参考这个链接
as per this
按照这
dp
dp
Density-independent Pixels - An abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi (dots per inch) screen, on which 1dp is roughly equal to 1px. When running on a higher density screen, the number of pixels used to draw 1dp is scaled up by a factor appropriate for the screen's dpi. Likewise, when on a lower density screen, the number of pixels used for 1dp is scaled down.
The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Using dp units (instead of px units) is a simple solution to making the view dimensions in your layout resize properly for different screen densities. In other words, it provides consistency for the real-world sizes of your UI elements across different devices.
与密度无关的像素——基于屏幕物理密度的抽象单元。这些单位相对于160 dpi(点/英寸)屏幕,1dp大约等于1px。当在更高密度的屏幕上运行时,用于绘制1dp的像素的数量会根据屏幕上的dpi来放大。同样,在密度较低的屏幕上,使用1dp的像素数量也会缩小。dpl与像素的比值将随屏幕密度的变化而变化,但不一定成正比。使用dp单元(而不是px单元)是一种简单的解决方案,可以让布局中的视图尺寸根据不同的屏幕密度适当调整大小。换句话说,它为跨不同设备的UI元素的实际大小提供了一致性。
px
px
Pixels - Corresponds to actual pixels on the screen. This unit of measure is not recommended because the actual representation can vary across devices; each devices may have a different number of pixels per inch and may have more or fewer total pixels available on the screen.
像素——对应于屏幕上的实际像素。不推荐使用这种度量单位,因为不同设备的实际表示可能不同;每台设备每英寸可能有不同数量的像素,屏幕上可能有更多或更少的可用像素。
#7
1
You can use getDimensionPixelOffset() instead of getDimension, so you didn't have to cast to int.
您可以使用getdimensions pixeloffset()而不是getDimension,因此不必强制转换为int类型。
int valueInPixels = getResources().getDimensionPixelOffset(R.dimen.test)
#1
629
In my dimens.xml I have
在我dimens。xml我有
<dimen name="test">48dp</dimen>
In code If I do
用代码
int valueInPixels = (int) getResources().getDimension(R.dimen.test)
this will return 72 which as docs state is multiplied by density of current phone (48dp x 1.5 in my case)
这将返回72作为文档状态乘以当前电话的密度(48dp x 1.5在我的例子中)
exactly as docs state :
如文件所述:
Retrieve a dimensional for a particular resource ID. Unit conversions are based on the current DisplayMetrics associated with the resources.
获取特定资源ID的维度。单元转换基于与资源关联的当前DisplayMetrics。
so if you want exact dp value just as in xml just divide it with DisplayMetrics density
如果你想要准确的dp值就像在xml中一样用DisplayMetrics密度来划分
int dp = (int) (getResources().getDimension(R.dimen.test) / getResources().getDisplayMetrics().density)
dp will be 48 now
dp现在是48。
#2
22
The Resource
class also has a method getDimensionPixelSize() which I think will fit your needs.
Resource类还有一个getdimensional pixelsize()方法,我认为它可以满足您的需要。
#3
17
Context.getResources().getDimension(int id);
#4
7
For those who just need to save some int
value in the resources, you can do the following.
对于那些只需要在资源中节省一些int值的人,您可以执行以下操作。
integers.xml
integers.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="default_value">100</integer>
</resources>
Code
代码
int defaultValue = getResources().getInteger(R.integer.default_value);
#5
2
You can write integer in xml file also..
have you seen [this] http://developer.android.com/guide/topics/resources/more-resources.html#Integer ? use as .
您也可以在xml文件中写入integer。你看过[this] http://developer.android.com/guide/topics/resources/more resources.html#Integer吗?使用。
context.getResources().getInteger(R.integer.height_pop);
#6
1
This works but the value I get is multiplied times the screen density factor
(1.5 for hdpi, 2.0 for xhdpi, etc).
I think it is good to get the value as per resolution but if you not want to do this give this in px.......
我认为按分辨率得到的价值是好的,但是如果你不想这样做,请用px来表示……
Density-independent pixel (dp)
密度独立像素(dp)
A virtual pixel unit that you should use when defining UI layout, to express layout dimensions or position in a density-independent way. The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, which is the baseline density assumed by the system for a "medium" density screen. At runtime, the system transparently handles any scaling of the dp units, as necessary, based on the actual density of the screen in use. The conversion of dp units to screen pixels is simple: px = dp * (dpi / 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels.
You should always use dp units when defining your application's UI, to ensure proper display of your UI on screens with different densities.
在定义UI布局时应该使用的虚拟像素单元,以一种与密度无关的方式表示布局维度或位置。与密度无关的像素相当于160 dpi屏幕上的一个物理像素,这是系统假定的“中等”密度屏幕的基线密度。在运行时,系统根据实际使用的屏幕密度,透明地处理dp单元的任何缩放。dp单位到屏幕像素的转换很简单:px = dp * (dpi / 160)。例如,在240 dpi屏幕上,1 dp等于1.5物理像素。在定义应用程序的UI时,应该始终使用dp单元,以确保在不同密度的屏幕上正确显示UI。
I think it is good to change the value as per resolution but if you not want to do this give this in px.......
我认为改变每个分辨率的值很好,但是如果你不想这样做的话,就在px里做这个吧。
refer this link
请参考这个链接
as per this
按照这
dp
dp
Density-independent Pixels - An abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi (dots per inch) screen, on which 1dp is roughly equal to 1px. When running on a higher density screen, the number of pixels used to draw 1dp is scaled up by a factor appropriate for the screen's dpi. Likewise, when on a lower density screen, the number of pixels used for 1dp is scaled down.
The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Using dp units (instead of px units) is a simple solution to making the view dimensions in your layout resize properly for different screen densities. In other words, it provides consistency for the real-world sizes of your UI elements across different devices.
与密度无关的像素——基于屏幕物理密度的抽象单元。这些单位相对于160 dpi(点/英寸)屏幕,1dp大约等于1px。当在更高密度的屏幕上运行时,用于绘制1dp的像素的数量会根据屏幕上的dpi来放大。同样,在密度较低的屏幕上,使用1dp的像素数量也会缩小。dpl与像素的比值将随屏幕密度的变化而变化,但不一定成正比。使用dp单元(而不是px单元)是一种简单的解决方案,可以让布局中的视图尺寸根据不同的屏幕密度适当调整大小。换句话说,它为跨不同设备的UI元素的实际大小提供了一致性。
px
px
Pixels - Corresponds to actual pixels on the screen. This unit of measure is not recommended because the actual representation can vary across devices; each devices may have a different number of pixels per inch and may have more or fewer total pixels available on the screen.
像素——对应于屏幕上的实际像素。不推荐使用这种度量单位,因为不同设备的实际表示可能不同;每台设备每英寸可能有不同数量的像素,屏幕上可能有更多或更少的可用像素。
#7
1
You can use getDimensionPixelOffset() instead of getDimension, so you didn't have to cast to int.
您可以使用getdimensions pixeloffset()而不是getDimension,因此不必强制转换为int类型。
int valueInPixels = getResources().getDimensionPixelOffset(R.dimen.test)