Android - 如何使 UI 在不同设备上看起来相同?
市场上有各种各样的设备,具有不同的屏幕尺寸、分辨率和像素密度。
通常用于确保应用程序在不同设备上看起来相同的一些布局首选项或提示/技巧是什么?
谢谢 克里斯
There is a variety of devices on the market, with different screen sizes, resolutions and pixel density.
What are some layout preferences or tips/tricks that are generally used to make sure the app looks the same across devices?
Thanks
Chris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我总是使用“dip”作为标准统一而不是像素,
使用layout_weight来制定比例
,我的所有应用程序都完全兼容10英寸平板设备和3.2英寸屏幕的小型智能手机!
I always use the "dip" as standart unity instead of pixels,
use layout_weight to make proportions
and all my app's are fully compatible to 10 inches tablet devices and small smartphones with 3,2 inches screen!
首先,您可以尝试根据屏幕的百分比定位和缩放 UI 元素(例如,将按钮放置在屏幕的 75% 处)。在其他情况下,例如屏幕右侧有一个窗口,只需获取屏幕宽度减去窗口大小并将其放置在那里。
For a start, you could try positioning and scaling UI elements based of a percentage of the screen (for example, placing a button at 75% of the screen). In other circumstances, for example having a window attached to the right of the screen, just get the width of the screen minus the size of the window and place it there.
确保您掌握布局上
wrap_content
和fill_parent
之间的区别。在垂直排列的元素(如列表)中,您几乎总是希望wrap_content
位于高度,fill_parent
位于宽度。对于水平排列的元素反之亦然。正如 Profete162 所说,总是更喜欢
dip
单位(它可以被认为是抽象的像素大小)。它有助于确保不同的分辨率密度不会破坏硬编码的宽度和高度。理想情况下,主题代码中不应提及dip
,但理论上的好处在实践中并不总是实用的。如今,您永远不应该使用
px
来表示某物的大小。Make sure you grasp the different between
wrap_content
andfill_parent
on the layouts. In in elements that are arranged vertically (like a list), you will almost always wantwrap_content
on the height andfill_parent
on the width. Vice versa for elements arranged hortizontally.As Profete162 said, always prefer the
dip
unit (it can be thought of as an abstracted pixel size). It helps insure that different resolution-densities don't break your hard-coded widths and heights. Ideally, there should never be a mention ofdip
in your theme code, but what is good in theory is not always pragmatic in practice.These days, you should never use
px
for the size of something.