从源代码中加载res / values / dimension.xml中的维度值

本文翻译自:Load dimension value from res/values/dimension.xml from source code

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 . 我有两个dimension.xml文件,一个在/res/values/dimension.xml ,另一个在/res/values-sw360dp/dimension.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”结尾的字符串......


#1楼

参考:https://stackoom.com/question/kf5Q/从源代码中加载res-values-dimension-xml中的维度值


#2楼

Context.getResources().getDimension(int id);

#3楼

    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. 在定义UI布局时应使用的虚拟像素单元,以与密度无关的方式表达布局尺寸或位置。 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. 与密度无关的像素相当于160 dpi屏幕上的一个物理像素,这是系统为“中等”密度屏幕假定的基线密度。 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. 在运行时,系统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.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.需要透明地处理dp单元的任何缩放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. 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时,应始终使用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. 这些单位相对于160 dpi(每英寸点数)的屏幕,其中1dp大致等于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. dp与像素的比率将随着屏幕密度而变化,但不一定成正比。 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. 使用dp单位(而不是px单位)是一种简单的解决方案,可以使布局中的视图尺寸适当调整以适应不同的屏幕密度。 In other words, it provides consistency for the real-world sizes of your UI elements across different devices. 换句话说,它为不同设备的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. 每个设备可以具有每英寸不同数量的像素,并且可以在屏幕上具有更多或更少的总像素。


#4楼

Resource类还有一个方法getDimensionPixelSize() ,我认为它符合您的需求。


#5楼

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. 检索特定资源ID的维度。 Unit conversions are based on the current DisplayMetrics associated with the resources. 单位转换基于与资源关联的当前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


#6楼

You can write integer in xml file also.. 你也可以在xml文件中写整数..
have you seen [this] http://developer.android.com/guide/topics/resources/more-resources.html#Integer ? 你见过[this] http://developer.android.com/guide/topics/resources/more-resources.html#Integer use as . 用于 。

 context.getResources().getInteger(R.integer.height_pop);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值