---
title: "android 学习2 imageView"
author: "ericjoe"
date: "2009-10-09T16:41:05Z"
tags: []
categories: ["移动开发"]
slug: "android-2-imageview"
draft: false
lastmod: "2009-10-09T16:42:03Z"
id: 483634
url: "https://coffeef.iteye.com/blog/483634"
---

有两种方式

 

 首先找一个png的图片放在res/drawable文件架下，文件名和扩展名全部用小写（注意），我的文件名称：android.png

 

  1.code方式：

 

```
ImageView imageView = new ImageView(this);

 imageView.setImageResource(R.drawable.android);//R.drawable.android后面的android是你的图片文件名

 setContentView(imageView);

```

 

        

  2，xm方式：

  修改main.xml

  添加：

 &lt;ImageView  
     android:id="@+id/imageview"  
     android:layout\_height="wrap\_content" //图片原来的size 如果用fill\_parent 就会拉伸  
    android:layout\_width="wrap\_content"  
     android:src="@drawable/android" //注意文件名  
    /&gt;

  代码加入：setContentView(R.layout.main);即可

运行：

![](http://dl.iteye.com/upload/picture/pic/46543/8046fa21-bb73-3612-b4af-b986c4b18742.png)