【问题标题】:Change image on click and make it fit in an ImageView单击更改图像并使其适合 ImageView
【发布时间】:2011-08-31 19:03:17
【问题描述】:

我目前正在从事我的 Android 个人项目。我是android开发的新手,自从我开始自学以来已经有几个月了,但我就是不能专注于这个。好吧,这是我的问题,首先我将讨论到目前为止我所做的:

我在表格布局中为每个 tableRow(六个表格行)放置了 6 个 ImageView,使其成为 6x6 ImageView。

每个 imageView 都有一个默认的图像图标(来自 android drawable 的默认图标)。

单击 ImageView 后,我将使用 setImageDrawable 更改它的 Image。

android 的默认图标大小小于我放置的图像,我认为 36x36 像素是默认图标(android 默认),其中我的图像大小为 80x80 像素。

现在问题来了:

单击图像后,它会成功更改图像,但也会将 imageView 的大小调整为我的图像大小(80x80 像素)。

那么我该如何克服呢?我已经尝试将 ImageView 的比例类型设置为 fitXY,但它仍然没有做任何事情,我的 imageView 已调整大小。

我也尝试将 wrap_content 更改为 fill_parent 但也没有任何反应(我只是认为它可以解决问题,哈哈)

这是我的 ImageView 的属性:

<ImageView android:scaleType="fitXY" 
           android:id="@+id/imageView1" 
           android:src="@drawable/image0" 
           android:layout_height="wrap_content" 
           android:layout_width="wrap_content">
</ImageView>

有什么帮助吗?提前致谢!

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    您可以将图像视图的固定大小设置为 xml 文件或通过编码例如在xml文件中

    <ImageView android:scaleType="fitXY" 
               android:id="@+id/imageView1" 
               android:src="@drawable/image0" 
               android:layout_height="90dp" 
               android:layout_width="120dp">
    </ImageView>
    

    所以所有的图片都以 120*90 的尺寸显示,所以当你改变图片时它会适合这个 imageview

    【讨论】:

    • 感谢您的帮助,顺便问一下,如果我将我的项目部署到分辨率更高的手机上怎么办?例如:从 320x480 像素到 600x1024 像素。如果是手机,会不会造成一些问题,比如在某些部件上造成死区?我还没有尝试部署它。再次感谢。
    猜你喜欢
    • 2017-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-16
    • 1970-01-01
    • 2011-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多