`

android 学习2 imageView

阅读更多

有两种方式

 

 首先找一个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

  添加:

 <ImageView
     android:id="@+id/imageview"
     android:layout_height="wrap_content" //图片原来的size 如果用fill_parent 就会拉伸
    android:layout_width="wrap_content"
     android:src="@drawable/android" //注意文件名
    />

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

运行:

3
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics