【问题标题】:Resize ImageView/ImageButton on different screen sizes with same width and height ratio在具有相同宽度和高度比的不同屏幕尺寸上调整 ImageView/ImageButton 的大小
【发布时间】:2013-05-29 07:12:06
【问题描述】:

我有由 4 个 ImageView/ImageButtons 组成的 Linearlayout。所有图像具有相同的宽度和高度。我希望此布局始终适合父母的宽度,并且所有图像都以相同的纵横比(宽度和高度)缩放:

我试过了:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/item_background"
            android:layout_weight="1"
            android:adjustViewBounds="true"/>
    // All 4 images are the same 
</LinearLayout>

问题是陆地/肖像中的宽高比不一样。例如在横向模式下,图像仅在 x 轴上拉伸,而不在 y 轴上拉伸:

我不想为这些只有 UI 的东西编写代码,我希望你能帮助我找到一种在 XML 中执行此操作的方法!

编辑:

将 android:scaleType="fitXY" 添加到图像后,我得到了这个:

如您所见,这不是我想要的一个好的解决方案。

【问题讨论】:

    标签: android android-layout android-imageview


    【解决方案1】:

    你可以使用

    android:scaleType="fitXY" 
    

    它将适合您的 ImageView 中的资源。

    【讨论】:

      【解决方案2】:

      使用android:layout_width="match_parent"android:scaleType="FIT_CENTER"

      【讨论】:

        【解决方案3】:

        这可能是正常的,因为您的线性布局的方向是“水平的”。

        要解决您的问题,您可以使用上面回答的代码。

        【讨论】:

        • 我将所有图片的 layout_weight 设置为 1。所以这不是问题。
        • 我认为你上面的代码在横向模式下很好,我试过了,没问题。 android:adjustViewBounds="true" 保留图像的纵横比
        【解决方案4】:

        有一个为android开发的.9.png图像格式,它可以帮助你解决这个问题,这是来自android开发者网站的链接Draw9patch

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-11-16
          • 2017-07-22
          • 2019-03-05
          • 2015-05-28
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多