Android可以支持小于1dp的dp吗? [英] Can Android support dp's less than 1dp?
问题描述
我正在制作日历,我需要一个网格.问题是边界,或者说是每个网格之间的空间(1 dp)是我用来模拟网格的方式.但是它比较厚.我正在查看具有边框的其他日历应用程序,它们非常薄.即使我使用drawable
形状并将其设置为1dp,它仍然具有该厚度.我尝试使用.5,但这似乎不起作用.那不可能吗?
I am making a calendar and I need a grid. Problem is the borders, or rather the space between each grid, which is what I am using to kind of simulate a grid, is 1 dp. But its rather thick. I am looking at other calendar apps that have the borders, and they are very thin. Even if I were to use a drawable
shape and make it 1dp, it still has that thickness. I tried using .5, but that did not seem to work. Is that not possible?
推荐答案
Android确实允许您将dp
的分数值输入XML,尽管我不确定我会推荐这样做,因为结果变得难以预测/计算.设备使用以下公式(基本上)将dp
值转换为像素:
Android does allow you to enter fractional values for dp
into XML, though I'm not sure I would recommend it because the results become less easy to predict/compute. Devices convert dp
values into pixels using (basically) the following formula:
px = (int)(scale * dp + 0.5)
(即设备密度标度四舍五入到最接近的整个像素值)
(i.e. the device density scale rounded to the nearest whole pixel value)
scale
值将基于设备的屏幕密度:
The scale
value would be based on the screen density of the device:
- MDPI = 1
- HDPI = 1.5
- XHDPI = 2
- XXHDPI = 3
因此,0.5dp
将导致上述密度的{1px,1px,1px,2px},而1dp
将是{1px,2px,2px,3px}.像0.1dp
这样的微小值将解析为{1px,1px,1px,1px},因为除非公式中小于c7的值,否则上述公式中小于1的任何内容都会解析为单个像素(无法绘制比单个像素宽度).
So 0.5dp
would result in {1px, 1px, 1px, 2px} for the above densities, whereas 1dp
would be {1px, 2px, 2px, 3px}. A tiny value like 0.1dp
would resolve to {1px, 1px, 1px, 1px} because anything less than 1 in the above formula resolves to a single pixel unless the value was explicitly 0dp
(it's not possible to draw something thinner than a single pixel width).
如果要确保使用尽可能薄的宽度,最好使用px
而不是像dp
这样的缩放单位明确定义宽度.设置1px
的值将在所有设备上绘制一个像素,并且比希望0.5dp
或0.1dp
做相同的操作更具可读性.
If you want to ensure that thinnest possible width is used, probably best to define the width explicitly with px
instead of a scaled unit like dp
. Setting a value of 1px
will draw with a single pixel on all devices, and is much more readable than hoping 0.5dp
or 0.1dp
does the same.
这篇关于Android可以支持小于1dp的dp吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!