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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11121028/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:04:17  来源:igfitidea点击:

Load dimension value from res/values/dimension.xml from source code

androidandroid-resourcesdpidimension

提问by Héctor Júdez Sapena

I'd like to load the value as it is. I have two dimension.xmlfiles, one in /res/values/dimension.xmland 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”结尾的字符串......

回答by AndroidGecko

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

回答by Jug6ernaut

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

回答by Ovidiu Latcu

The Resourceclass also has a method getDimensionPixelSize()which I think will fit your needs.

Resource班也有一个方法getDimensionPixelSize() ,我认为会满足您的需求。

回答by Suragch

For those who just need to save some intvalue in the resources, you can do the following.

对于那些只需要int在资源中节省一些价值的人,您可以执行以下操作。

integers.xml

整数.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);

回答by Jonny Right

You can use getDimensionPixelOffset()instead of getDimension, so you didn't have to cast to int.

您可以使用getDimensionPixelOffset()而不是 getDimension,因此您不必强制转换为 int。

int valueInPixels = getResources().getDimensionPixelOffset(R.dimen.test)

回答by AndroidLad

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 文件中写整数..
你见过 [this] http://developer.android.com/guide/topics/resources/more-resources.html#Integer吗?用于 。

 context.getResources().getInteger(R.integer.height_pop);

回答by Dheeresh Singh

    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 单位的任何缩放。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.在定义应用程序的 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。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.dp-to-pixel的比例会随着屏幕密度的变化而变化,但不一定成正比。使用 dp 单位(而不是 px 单位)是使布局中的视图尺寸针对不同的屏幕密度正确调整大小的简单解决方案。换句话说,它为您的 UI 元素在不同设备上的真实尺寸提供了一致性。

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.

像素 - 对应于屏幕上的实际像素。不建议使用此度量单位,因为实际表示可能因设备而异;每个设备每英寸可能有不同数量的像素,并且屏幕上可用的总像素可能更多或更少。

回答by Gibolt

Use a Kotlin Extension

使用 Kotlin 扩展

You can add an extension to simplify this process. It enables you to just call context.dp(R.dimen. tutorial_cross_marginTop)to get the Float value

您可以添加扩展来简化此过程。它使您只需调用context.dp(R.dimen. tutorial_cross_marginTop)即可获取 Float 值

fun Context.px(@DimenRes dimen: Int): Int = resources.getDimension(dimen).toInt()

fun Context.dp(@DimenRes dimen: Int): Float = px(dimen) / resources.displayMetrics.density

If you want to handle it without context, you can use Resources.getSystem():

如果你想在没有上下文的情况下处理它,你可以使用Resources.getSystem()

val Int.dp get() = this / Resources.getSystem().displayMetrics.density // Float

val Int.px get() = (this * Resources.getSystem().displayMetrics.density).toInt()

For example, on an xhdpi device, use 24.dpto get 12.0 or 12.pxto get 24

例如,在 xhdpi 设备上,使用24.dp获取 12.0 或12.px获取 24