博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
从源代码中加载res / values / dimension.xml中的维度值
阅读量:3582 次
发布时间:2019-05-20

本文共 5278 字,大约阅读时间需要 17 分钟。

本文翻译自:

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楼

参考:


#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 请参阅此

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类还有一个方法 ,我认为它符合您的需求。


#5楼

In my dimens.xml I have 在我的dimens.xml中,我有

48dp

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] ? 你见过[this] ? use as . 用于 。

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

转载地址:http://ymlgj.baihongyu.com/

你可能感兴趣的文章
MySQL编程 优化篇(六) 锁问题
查看>>
MySQL编程 优化篇(七) 优化MySQL Server
查看>>
Hive学习(五)DDL数据定义
查看>>
Hive学习(四)数据类型
查看>>
Hive学习(六)DML数据操作
查看>>
Hive学习(二)入门
查看>>
Hive学习(一)大数据基础知识介绍
查看>>
Hive学习(三)安装配置
查看>>
Hive学习(七)查询
查看>>
Hive学习(八)函数
查看>>
Hive学习(九)企业级调优
查看>>
MySQL 5.6 Keywords and Reserved Words(关键字和保留词)
查看>>
阿里云函数计算 使用Python开发一个基于WSGI的HTTP触发器 (实战)
查看>>
JSQLParser 解析 sql select 字段(含对别名的解析)
查看>>
如何在win10 下同时使用32位和64的eclipse
查看>>
MyBatis (五)一级缓存和二级缓存的区别
查看>>
MyBatis (六)结合多线程实现模拟缓存
查看>>
Oracle样例数据库(SCOTT)的SQL查询题
查看>>
Oracle程序题之:夺冠球队
查看>>
EL获取url传递的参数,并且根据参数设置弹出框
查看>>