【问题标题】:Changing layout_height to "wrap_content" shrinks components将 layout_height 更改为“wrap_content”会缩小组件
【发布时间】:2021-07-31 02:25:41
【问题描述】:

我了解最佳做法是将 UI 中的每个组件的 layout_width 设置为“match_parent”,并将 layout_height 设置为“wrap_content”,以确保在设备、纵向视图和横向视图之间正确缩放。但是,将 layout_height 设置为“wrap_content”会导致每个元素缩小。如何使元素保持所需的大小并仍然确保适当的缩放?

之前:

之后:

之后

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    您总是为每个组件定义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

    【讨论】:

    • 谢谢!不过,我不关心这里的图像。我只需要弄清楚如何让 Button 或 ScrollBar 之类的东西在每个设备上保持相同的宽度和高度。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-14
    • 2023-03-24
    • 1970-01-01
    • 2019-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多