引用自Google API Guides

Dimension


A dimension value defined in XML. A dimension is specified with a number followed by a unit of measure. For example: 10px, 2in, 5sp. The following units of measure are supported by Android:

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.
sp
Scale-independent Pixels - This is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and the user's preference.
pt
Points - 1/72 of an inch based on the physical size of the screen.
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.
mm
Millimeters - Based on the physical size of the screen.
in
Inches - Based on the physical size of the screen.

Note: A dimension is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). As such, you can combine dimension resources with other simple resources in the one XML file, under one <resources> element.

用法

FILE LOCATION:
res/values/filename.xml
The filename is arbitrary. The <dimen> element's name will be used as the resource ID.
RESOURCE REFERENCE:
In Java: R.dimen.dimension_name
In XML: @[package:]dimen/dimension_name
SYNTAX:
   1: <?xml version="1.0" encoding="utf-8"?>

   2: <resources>    

   3:     <dimen name="dimension_name">dimension</dimen>

   4: </resources>

ELEMENTS:

<resources>

Required. This must be the root node.

No attributes.

<dimen>

A dimension, represented by a float, followed by a unit of measurement (dp, sp, pt, px, mm, in), as described above.

attributes:

name

String. A name for the dimension. This will be used as the resource ID.
EXAMPLE:

XML file saved at res/values/dimens.xml:

   1: <?xml version="1.0" encoding="utf-8"?>

   2: <resources>    

   3:     <dimen name="textview_height">25dp</dimen>    

   4:     <dimen name="textview_width">150dp</dimen>    

   5:     <dimen name="ball_radius">30dp</dimen>    

   6:     <dimen name="font_size">16sp</dimen>

   7: </resources>

This application code retrieves a dimension:

Resources res = getResources();
float fontSize = res.getDimension(R.dimen.font_size);

This layout XML applies dimensions to attributes:

   1: <TextView    

   2:     android:layout_height="@dimen/textview_height"    

   3:     android:layout_width="@dimen/textview_width"    

   4:     android:textSize="@dimen/font_size"/>

 

常见的密度比值:

QVGA:240*320 的密度比值是: 0.75

HVGA:320*480 的密度比值是: 1.0

WVGA:480*800 的密度比值是: 1.5

计算方式

float density = getResources().getDisplayMetrics().density;

1.0 * 160dp = 160px HVGA

0.75 * 160dp = 120px QVGA

1.5 * 160dp = 240px WVGA

最新文章

  1. java基础知识(四)java内存机制
  2. eclipse智能提示
  3. 折腾了好久的macos+apache+php+phpmyadmin 终于成功了!
  4. Windows7 64位系统下无法安装网络打印机的解决方法
  5. 【BZOJ 1491】 [NOI2007]社交网络
  6. 软件测试模型汇总-V模型,W模型,X模型,H模型
  7. 【HDOJ】2853 Assignment
  8. 买面包和IoC
  9. 大学二三事&mdash;&mdash;那些人(1)
  10. Mac linux 安装memcached服务 用法
  11. Mego开发文档 - 保存关系数据
  12. ajax 与jsp servlet
  13. [Split The Tree][dfs序+树状数组求区间数的种数]
  14. 从零开始学习html(三) 认识标签(第二部分)
  15. JS版日期格式化和解析工具类,毫秒级
  16. mongodb部署
  17. Unity3D学习笔记(五):坐标系、向量、3D数学
  18. linux I2C_client产生方法一
  19. 织梦ask标签的调用
  20. ny523 亡命逃串 hdoj 1253胜利大逃亡

热门文章

  1. loadrunner使用system()函数调用Tesseract-OCR识别验证码遇到的问题
  2. Unity打包APK横屏时的注意事项
  3. Intellij IDEA采用Maven+Spring MVC+Hibernate的架构搭建一个java web项目
  4. 关于Javascript函数的几点笔记
  5. 课程设计之(struts2+Hibernate)航空订票系统
  6. Linux 删除文件夹和创建文件的命令
  7. Java类型
  8. jboolean
  9. textbox不支持Ctrl+A
  10. git跨平台换行符不兼容