【问题标题】:How to set the size of an imageView如何设置imageView的大小
【发布时间】:2014-02-28 05:52:36
【问题描述】:

我的布局中有 2 个像这样定义的图像视图

<ImageView
    android:id="@+id/image1"
    android:layout_width="320dp"
    android:layout_height="200dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/image1"
    android:contentDescription="@string/image1" />

<ImageView
    android:id="@+id/image2"
    android:layout_width="320dp"
    android:layout_height="220dp"
    android:layout_alignLeft="@+id/image1"
    android:layout_alignParentBottom="true"
    android:src="@drawable/image2"
    android:contentDescription="@string/image2" />

在模拟器上它工作正常,但是当我在平板电脑上测试应用程序时,图像 ramain 320x220。 如何调整图片大小?

【问题讨论】:

  • 请说明问题所在。

标签: android


【解决方案1】:

原因是因为许多平板电脑的密度与手机相同(mdpi、xhdpi),所以dp 单位解析为相同数量的像素。将图像大小设置为 dimens 资源,您可以针对不同的最小屏幕尺寸进行控制。换句话说:

res/layout/your_layout.xml

<ImageView
    android:id="@+id/image1"
    android:layout_width="@dimen/image_width"
    android:layout_height="@dimen/image_height"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/image1"
    android:contentDescription="@string/image1" />

res/values/dimens.xml

<resources>
    <dimen name="image_width">320dp</dimen>
    <dimen name="image_height">200dp</dimen>
</resources>

res/values-sw600dp/dimens.xml

<resources>
    <dimen name="image_width">640dp</dimen>
    <dimen name="image_height">400dp</dimen>
</resources>

较大的值将用于最小宽度为 600dp 或更大的屏幕(平均 7 英寸平板电脑),而较小的值将在其他任何地方用作默认值。如果要添加更多离散更改,可以创建附加 dimens 以覆盖特定用例中的默​​认值。

【讨论】:

  • 对于屏幕更大的智能手机?
  • 添加尽可能多的不同尺寸定义,当图像“dp”尺寸发生变化时,您认为需要满足这些定义。这只是一个例子。
【解决方案2】:

您的意思是您想要 different devices 的大小不同?像手机的 320x200 但平板电脑的 640x400?

您需要有不同的布局文件。

或者您可以通过编程更改大小/布局/任何内容:https://stackoverflow.com/a/15171766/29505

【讨论】:

    【解决方案3】:

    要添加到 Devunwired 答案,您还必须为 10" 设备添加 res/values-sw720dp/dimens.xml。所以您应该拥有三个:

    res/values/dimens.xml (standard screen)
    
    res/values-sw600dp/dimens.xml (7" devices)
    
    res/values-sw720dp/dimens.xml (10" devices)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多