如何更改 ImageView 的图像?
我刚刚开始学习android。我不知道如何更改 ImageView
的图像?即它有一些在布局中设置的图像,但我想通过编码更改该图像我应该怎么做?
这是 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"/>
感谢您的回复。
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 ?
Here is the 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"/>
thanks for replying.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您使用 xml 文件创建 imageview,请按照以下步骤操作。
解决方案 1:
步骤 1:创建 XML 文件
步骤 2:创建 Activity
解决方案 2:
如果您从 Java 类创建了 imageview
If you created imageview using xml file then follow the steps.
Solution 1:
Step 1: Create an XML file
Step 2: create an Activity
Solution 2:
If you created imageview from Java Class
查看 ImageView API。有多种
setImage*
方法。使用哪一个取决于您提供的图像。如果您有图像作为资源(例如文件 res/drawable/my_image.png)Have a look at the ImageView API. There are several
setImage*
methods. Which one to use depends on the image you provide. If you have the image as resource (e.g. file res/drawable/my_image.png)更进一步,您还可以直接设置位图,如下所示:
当然,此技术仅在需要更改图像时才有用。
Just to go a little bit further in the matter, you can also set a bitmap directly, like this:
Of course, this technique is only useful if you need to change the image.