Android-ImageView的属性
从去年的10月开始学习安卓,差不多快半年了,感觉自己半桶水,所以又重新开始复习以前的基础知识,
发现自己真的还有很多基础概念不清晰,唉。
ImageView 从名字上看出,这是安卓来显示图片的组件,可以引用图片资源在安卓设备上进行显示,不过
需要注意的是,显示有两种方式,(我以前就是懵逼了…..)
<ImageView
android:id="@+id/id_image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
/>
<ImageView
android:id="@+id/id_image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@mipmap/ic_launcher"
/>
效果:
可以看出没什么不一样,修改一下它们的宽度:
android:layout_width="match_parent"
效果:
可以看到有很大的变化。
原因:因为第一张图片是src方式进行引用,是作为图片的资源,但是第二张是以backgroud进行引用,
它是作为背景的,所以宽度自然会充满整个屏幕。另外background还可以用来设置背景色
例子:
<ImageView
android:id="@+id/id_image3"
android:layout_width="match_parent"
android:layout_height="12dp"
android:background="#FF00FF00"
/>
效果:
简而言之
background会根据ImageView组件给定的长宽进行拉伸,而src就存放的是原图的大小,不会进行拉伸。src是图片内容(前景),bg是背景,可以同时使用。
所以两者可以共存
<ImageView
android:id="@+id/id_image1"
android:background="#44ff0000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
/>
效果:
总结:如果你想使用图片作为图标的话,就使用src方式,如果想设置背景图片或者背景色,就使用background。