多设备官方教程(3)多屏幕(上)用密度不用像素
Supporting Different Densities
This lesson shows you how to support different screen densities by providing different resources and using resolution-independent units of measurements.
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 either dp
or sp
units. A dp
is a density-independent pixel that corresponds to the physical size of a pixel at 160 dpi. An sp
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).
VIDEO
DesignBytes: Density-independent Pixels
For example, when you specify spacing between two views, use dp
rather than px
:
1 <Button android:layout_width="wrap_content" 2 android:layout_height="wrap_content" 3 android:text="@string/clickme" 4 android:layout_marginTop="20dp" />
When specifying text size, always use sp
:
1 <TextView android:layout_width="match_parent" 2 android:layout_height="wrap_content" 3 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.0hdpi
: 1.5mdpi
: 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 for hdpi
, 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.
For more tips and guidelines for creating icon assets for your application, see the Icon Design Guidelines.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?