【发布时间】:2021-07-31 02:25:41
【问题描述】:
我了解最佳做法是将 UI 中的每个组件的 layout_width 设置为“match_parent”,并将 layout_height 设置为“wrap_content”,以确保在设备、纵向视图和横向视图之间正确缩放。但是,将 layout_height 设置为“wrap_content”会导致每个元素缩小。如何使元素保持所需的大小并仍然确保适当的缩放?
之前:
之后:
之后
【问题讨论】:
我了解最佳做法是将 UI 中的每个组件的 layout_width 设置为“match_parent”,并将 layout_height 设置为“wrap_content”,以确保在设备、纵向视图和横向视图之间正确缩放。但是,将 layout_height 设置为“wrap_content”会导致每个元素缩小。如何使元素保持所需的大小并仍然确保适当的缩放?
之前:
之后:
之后
【问题讨论】:
您总是为每个组件定义layout_width match_parent 和高度wrap_content 并不是一个经验法则。这完全取决于您的设计。
要在具有不同像素密度的设备上提供良好的图形质量,您应该在应用中提供每个图像的多个版本——每个密度桶一个版本,具有相应的分辨率。否则,Android 必须缩放您的图像,使其在每个屏幕上占据相同的可见空间,从而导致缩放伪影,例如模糊。
您的应用中有多种密度桶可供使用。
ldpi Resources for low-density (ldpi) screens (~120dpi).
mdpi Resources for medium-density (mdpi) screens (~160dpi). (This is the baseline density.)
hdpi Resources for high-density (hdpi) screens (~240dpi).
xhdpi Resources for extra-high-density (xhdpi) screens (~320dpi).
xxhdpi Resources for extra-extra-high-density (xxhdpi) screens (~480dpi).
xxxhdpi Resources for extra-extra-extra-high-density (xxxhdpi) uses (~640dpi).
nodpi Resources for all densities.
这些是与密度无关的资源。无论当前屏幕的密度如何,系统都不会缩放带有此限定符标记的资源。
或者您可以使用 SVG 矢量而不是 PNG,它会减小 APK 的大小。 https://developer.android.com/guide/topics/graphics/vector-drawable-resources
【讨论】: