如何更改ImageView的图像? [英] How can I change the image of an ImageView?

查看:113
本文介绍了如何更改ImageView的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习android.而且我不知道如何更改ImageView的图像?即它有一些在布局中设置的图像,但是我想通过编码更改该图像,我应该怎么做?

I have just started learning android. And i don't know How can I change the image of an ImageView? ie it has some Image which was set in the layout but i want to change that image through coding how should i do it ?

这是xml文件

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="#cc8181"
 >

<ImageView
  android:id="@+id/image"
  android:layout_width="50dip"
  android:layout_height="fill_parent" 
  android:src="@drawable/icon"
  android:layout_marginLeft="3dip"
  android:scaleType="center"/>

感谢您的答复.

推荐答案

如果您使用xml文件创建了imageview,请按照以下步骤操作.

If you created imageview using xml file then follow the steps.

解决方案1:

第1步:创建XML文件

Step 1: Create an XML file

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="#cc8181"
  >

  <ImageView
      android:id="@+id/image"
      android:layout_width="50dip"
      android:layout_height="fill_parent" 
      android:src="@drawable/icon"
      android:layout_marginLeft="3dip"
      android:scaleType="center"/>

</LinearLayout>

第2步:创建活动

ImageView img= (ImageView) findViewById(R.id.image);
img.setImageResource(R.drawable.my_image);

解决方案2:

如果您是通过Java类创建的imageview

If you created imageview from Java Class

ImageView img = new ImageView(this);
img.setImageResource(R.drawable.my_image);

这篇关于如何更改ImageView的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆