代码之家  ›  专栏  ›  技术社区  ›  Leonid Ratanov

如何使图像库应用程序在所有不同的屏幕大小上看起来相同?

  •  0
  • Leonid Ratanov  · 技术社区  · 8 年前

    我正在开发一个类似于图像库的应用程序,从网络上获取图像。所有图像具有相同的宽度 200px 和高度 280-300px .我使用 RecyclerView 具有 Adapter.ViewHolder GridLayoutManager 跨度计数=2。图像加载 Picasso 图书馆

    item_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <ImageView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tile_picture"
        android:layout_width="match_parent"
        android:layout_height="260dp"
        android:layout_margin="1dp"
        android:scaleType="centerCrop"/>
    

    在我的设备上 5" 720x1280 看起来很好。 Screenshot 但当我在其他设备上启动应用程序时,它看起来不太好。。。 在…上 5.5" 1080x1920 图像之间有额外的空白。 旧的 3.7" 480x800 只有一个跨度,宽度略大于半屏幕大小。 我建议,因为 android:layout_height="260dp" 硬编码。 所以,如何在不同的设备上获得相同的外观?

    1 回复  |  直到 8 年前
        1
  •  0
  •   Ari M    8 年前

    我可以建议您实现自定义视图,并在 onMeasure 获取屏幕宽度,然后通过一半宽度:

    setMeasuredDimension(width/2, height);

    这将保证在任何设备上,屏幕的一半都有图像。