Use Density-independent Pixels
One common pitfall you must avoid when designing your layouts is using absolute pixels to define distances or sizes. Defining layout dimensions with pixels is a problem because different screens have different pixel densities, so the same number of pixels
may correspond to different physical sizes on different devices. Therefore, when specifying dimensions, always use eitherdp
or
sp
units. A dp
is a density-independent pixel that corresponds to the physical size of a pixel at 160 dpi. Ansp
is the same base unit, but is scaled by the user's preferred text size (it’s a scale-independent pixel), so
you should use this measurement unit when defining text size (but never for layout sizes).
For example, when you specify spacing between two views, use dp
rather thanpx
:
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/clickme"
android:layout_marginTop="20dp" />
When specifying text size, always use sp
:
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp" />
Provide Alternative Bitmaps
Since Android runs in devices with a wide variety of screen densities, you should always provide your bitmap resources tailored to each of the generalized density buckets: low, medium, high and extra-high density. This will help you achieve good graphical
quality and performance on all screen densities.
To generate these images, you should start with your raw resource in vector format and generate the images for each density using the following size scale:
-
xhdpi
: 2.0
-
hdpi
: 1.5
-
mdpi
: 1.0 (baseline)
-
ldpi
: 0.75
This means that if you generate a 200x200 image for xhdpi
devices, you should generate the same resource in 150x150 forhdpi
, 100x100 for
mdpi
and finally a 75x75 image for ldpi
devices.
Then, place the generated image files in the appropriate subdirectory under
res/
and the system will pick the correct one automatically based on the screen density of the device your application is running on:
MyProject/
res/
drawable-xhdpi/
awesomeimage.png
drawable-hdpi/
awesomeimage.png
drawable-mdpi/
awesomeimage.png
drawable-ldpi/
awesomeimage.png
Then, any time you reference @drawable/awesomeimage
, the system selects the appropriate bitmap based on the screen's dpi.
分享到:
相关推荐
Android categorizes device screens using two general properties: ... As such, you should include some alternative resources that optimize your app’s appearance for different screen sizes and densities.
Android categorizes device screens using two general properties: ... As such, you should include some alternative resources that optimize your app’s appearance for different screen sizes and densities.
为了支持更多的语言,需要在values目录的名称需要以连字符“—”和国家的ISO码结尾.举个例子,values-es/目录中包含了语言代码和语言环境为“ES”的简单资源。Android在运行时会根据设备的语言环境设置来装载适当的...
TGac Channel Model Addendum Supporting Material
Supporting profiles: Networking, Dial-up, Fax, LAN access, Headset and WiFi
2006 Hinton 数据降维的支持文档
The supporting information fot JGR (Boxin Zuo)
sccm 2007 workbook
IntelliJ plugin for supporting PermissionsDispatcher.zip,支持PermissionsDispatcher的IntelliJ插件
开源GPGPU项目现已支持CUDA
A scalable storage supporting multistream real-time data retrieval
本书介绍了操作系统以及位于操作系统之上的其他软件(称为中间件)如何提供对计算,数据存储和网络通信的支持。
An online code compiler supporting 11 programming language
MindQuantum is a general software library supporting the development of applications for quantum computation.
A freeware HMI software package supporting Modbus and DNP3, including Slave simulator.
该文章阐述了西方国家如何使F-35战机的生存力大大提升。这是一个系统工程,被称为PHM的结构健康监测与反应机制。
TX1 for Supporting Real-time Computer-Vision Workloads 嵌入式视觉系统
Trusted Platform Module Library Part 4: Supporting Routines This part contains C code that describes the algorithms and methods used by the command code in TPM 2.0 Part 3. The code in this document ...
As you can see,indexers behave much like a custom collection supporting the IEnumeratorand IEnumerableinterfaces in that they provide access to a container’s subitems. The major difference ofcourse ...