引用自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

2.1……Android中的单位简介的更多相关文章

  1. Android中的单位及测试相关概念

    android中的单位: in 英寸 pt 点距 px 像素  dp(dip) 密度无关的像素单位,自适应device屏幕的比例,通常涉及长宽高时采用 sp 与范围无关的像素单位,通常在设置字体大小时 ...

  2. 关于android中的单位(dp、sp)

    android让人头疼的适配问题. --------- Android 中的单位大概有这些: 经常使用的dip.sp.有时候用到px. --------- 介绍两个类: TypedValue:有一些单 ...

  3. Android中的单位

    Android中的单位 1.px 像素(pixels) VGA 480*640像素 (Video Graphics Array) QVGA 240*320像素 (Quarter VGA) HVGA 3 ...

  4. android中像素单位dp、px、pt、sp的比较

    dp(dip): device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA.HVGA和QVGA 推荐使用这个,不依赖 ...

  5. Android 中的 Intent 简介

    Intent是Android程序中各组件之间进行交互的一种重要方式,它不仅可以指明当前组件想要执行的动作,还可以在不同组件之间传递数据. ------------------------------- ...

  6. Android中常用单位dp,px,sp之间的相互转换

    MainActivity如下: package cc.testunitswitch; import android.os.Bundle; import android.util.DisplayMetr ...

  7. 对Android中dp单位的理解

    dp 设备独立像素 ,也叫dip, device independent pixle. 比如同样在1英寸大小的屏幕上,高密度的屏幕可显示100个像素点,而低密度的屏幕只能70个点. 用了dp之后,只要 ...

  8. 【转】Android中dip(dp)与px之间单位转换

    Android中dip(dp)与px之间单位转换 dp这个单位可能对web开发的人比较陌生,因为一般都是使用px(像素)但是,现在在开始android应用和游戏后,基本上都转换成用dp作用为单位了,因 ...

  9. Android中SELinux的TE简介【转】

    转自:https://blog.csdn.net/murphykwu/article/details/52457667 selinux的概念如上一篇链接所示: http://www.cnblogs.c ...

随机推荐

  1. 进程内核栈、用户栈及 Linux 进程栈和线程栈的区别

    Linux 进程栈和线程栈的区别 http://www.cnblogs.com/luosongchao/p/3680312.html 总结:线程栈的空间开辟在所属进程的堆区,线程与其所属的进程共享进程 ...

  2. Android:PopupWindow简单弹窗

    两布局,一个当前布局页面和一个点击展示布局页面,主程序代码: public class MainActivity extends Activity { private PopupWindow pop; ...

  3. Intellij IDEA采用Maven+Spring MVC+Hibernate的架构搭建一个java web项目

    原文:Java web 项目搭建 Java web 项目搭建 简介 在上一节java web环境搭建中,我们配置了开发java web项目最基本的环境,现在我们将采用Spring MVC+Spring ...

  4. Java多线程2:实现多线程的两种方式

    原文:http://www.cnblogs.com/skywang12345/p/3479063.html 常见的实现多线程的方式有2种,一是继承Thread类,二是实现 Runnable接口,还可以 ...

  5. 浏览器 怪异模式(Quirks Mode) 与 标准模式(Standards Mode)

    浏览器 怪异模式(Quirks Mode) 与 标准模式(Standards Mode) 怪异模式,浏览器使用自己的方式解析渲染页面,在不同的浏览器就会显示不同的样式.标准模式,浏览器使用W3C的标准 ...

  6. apk反编译(4)Smali代码注入

    转自 : http://blog.sina.com.cn/s/blog_5674d18801019i89.html 应用场景 Smali代码注入只能应对函数级别的移植,对于类级别的移植是无能为力的.具 ...

  7. VS2012界面改为英文

    需要下载一个语言包 http://www.microsoft.com/en-us/download/confirmation.aspx?id=30681 还是不要指望这个,简直坑爹. 我把所有中文版的 ...

  8. TCP建立连接和释放的过程,及TCP状态变迁图

    一.TCP报文格式 下面是TCP报文格式图: 重要字段介绍: (1)序号:Seq序号,占32位,用来标识从TCP源端向目的端发送的字节流,发起方发送数据时对此进行标记. (2)确认序号:Ack序号,占 ...

  9. unique() 去重函数

    unique()函数是一个去重函数,STL中unique的函数 unique的功能是去除相邻的重复元素(只保留一个), 还有一个容易忽视的特性是它并不真正把重复的元素删除.他是c++中的函数, 所以头 ...

  10. asp.net清除页面缓存防止同时登录

    //清除页面缓存,防止页面回退重复提交数据 在页面里做以下设置就可以使页面的缓存失效,每次都需要获取新页面. Response.Cache.SetCacheability(System.Web.Htt ...