Android中Button,ImageButton,ImageView之间的区别
2013-07-03 10:18
316 查看
在Web开发中,HTML中对图片的操作就是提供一个<img>标签,我们通过该标签的src属性来制定图片资源的地址,从而在页面中显示一个图片。那么在Android中,ImageView就是用于图片显示的
代码非常的简单,ID就不多说了,然后设置长和宽,contentDescription是对图片的一个简要说明,最重要的src属性,这和HTML中的属性名称是一致的,也就是指定图片资源的位置。我们知道,Android项目中的图片资源统一位于res/drawable-xx文件夹内,不同的分辨率用于不同的设备,而我们只需通过@drawable/来调用图片资源即可。放置在drawable文件夹内的图片,会在R.java中自动注册,所以我们才能访问到其中的图片资源。
对于ImageView没有什么可以多说的内容,下面直接来看看ImageButton。
ImageButton就是图片按钮,在HTML中,我们定义按钮可以通过<button>标签来定义,表单中的按钮还可以是<submit>和<reset>,但是原生的HTML对按钮没有任何装饰,显示效果不好,我们可以通过CSS来美化按钮,比如加上图片。而在Android中,有ImageButton组件,可以为我们直接达到此目的。
java.lang.Object
↳ android.view.View
↳ android.widget.ImageView
↳ android.widget.ImageButton
可以注意到了,ImageButton是和Button没有任何关系的。Button类是TextView类的子类,而ImageButton类却是ImageView的子类。图片按钮,肯定需要图片,之前我们介绍过,Android的所有图片资源都在res/drawable-xx文件夹内,那么我们直接来看代码,把上面的ImageView的代码稍微修改一下:
很容易看出,就是在按钮上加了图片的效果,只是这里我们的图片比较小,按钮部分还留有显示,只要做出合适大小的按钮图片,那么ImageButton就全是图片显示了,只不过这个图片可以进行点击操作。关于ImageButton也没有需要多说的了,需要时会用就行了。
不同点:
↳ android.widget.ImageButton
java.lang.Object ↳ android.view.View ↳ android.widget.TextView ↳android.widget.Button
因而ImageButton 不支持setText,而Button支持。反之,ImageButton
支持setImageURI,而Button不支持。Button和ImageButton有Button的状态,但是ImageView没有。
ImageButton 拥有默认背景:
java.lang.Object ↳ android.view.View ↳ android.widget.ImageView可以看出,ImageView的继承结构并不复杂,它是View类的直接子类。下面就来看看关于ImageView的介绍,首先在Eclipse中重新创建一个项目,编写如下代码:
<ImageView android:id="@+id/img" android:layout_width="fill_parent" android:layout_height="wrap_content" android:contentDescription="ImageView_test" android:src="@drawable/ic_launcher" />
代码非常的简单,ID就不多说了,然后设置长和宽,contentDescription是对图片的一个简要说明,最重要的src属性,这和HTML中的属性名称是一致的,也就是指定图片资源的位置。我们知道,Android项目中的图片资源统一位于res/drawable-xx文件夹内,不同的分辨率用于不同的设备,而我们只需通过@drawable/来调用图片资源即可。放置在drawable文件夹内的图片,会在R.java中自动注册,所以我们才能访问到其中的图片资源。
对于ImageView没有什么可以多说的内容,下面直接来看看ImageButton。
ImageButton就是图片按钮,在HTML中,我们定义按钮可以通过<button>标签来定义,表单中的按钮还可以是<submit>和<reset>,但是原生的HTML对按钮没有任何装饰,显示效果不好,我们可以通过CSS来美化按钮,比如加上图片。而在Android中,有ImageButton组件,可以为我们直接达到此目的。
java.lang.Object
↳ android.view.View
↳ android.widget.ImageView
↳ android.widget.ImageButton
可以注意到了,ImageButton是和Button没有任何关系的。Button类是TextView类的子类,而ImageButton类却是ImageView的子类。图片按钮,肯定需要图片,之前我们介绍过,Android的所有图片资源都在res/drawable-xx文件夹内,那么我们直接来看代码,把上面的ImageView的代码稍微修改一下:
<ImageButton android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="ImageButton test" android:src="@drawable/ic_launcher" />
很容易看出,就是在按钮上加了图片的效果,只是这里我们的图片比较小,按钮部分还留有显示,只要做出合适大小的按钮图片,那么ImageButton就全是图片显示了,只不过这个图片可以进行点击操作。关于ImageButton也没有需要多说的了,需要时会用就行了。
不同点:
1.继承不同:
java.lang.Object ↳ android.view.View ↳android.widget.ImageView↳ android.widget.ImageButton
java.lang.Object ↳ android.view.View ↳ android.widget.TextView ↳android.widget.Button
因而ImageButton 不支持setText,而Button支持。反之,ImageButton
支持setImageURI,而Button不支持。Button和ImageButton有Button的状态,但是ImageView没有。
2.显示差异:
imageView 加上可点击的属性同样实现button的点击功能,这样看来没啥区别,可是可是,最近我做的listView,一个imagebutton,一个imageView 显示圆角图片的时候,imageBotton 居然显示不出来.ImageButton 拥有默认背景:
<style name="Widget.ImageButton"> <item name="android:focusable">true</item> <item name="android:clickable">true</item> <item name="android:scaleType">center</item> <item name="android:background">@android:drawable/btn_default</item> </style>
3.支持图片:ImageButton支持9.png 吧~imangeView就不行了。 |
相关文章推荐
- android中ImageView、ImageButton、Button之间的区别
- android中ImageView、ImageButton、Button之间的区别
- android中ImageView、ImageButton、Button之间的区别
- Android中ImageView、ImageButton、Button之间的区别
- android中ImageView、ImageButton、Button之间的区别
- android中ImageView、ImageButton、Button之间的区别
- Android学习指南之九:Button、TextView、EditView、CheckBox、RadioGroup、ImageView、ImageButton
- Android - 控件(View)之TextView, Button, ImageButton, ImageView, CheckBox
- Android ImageView,ImageButton 与 Button
- 系出名门Android(5) - 控件(View)之TextView, Button, ImageButton, ImageView, CheckBox
- 系出名门Android(5) - 控件(View)之TextView, Button, ImageButton, ImageView, CheckBox, RadioButton, AnalogClock, DigitalClock
- Android控件——TextView、EditText、ImageView、Button、ImageButton
- 系出名门Android(5) - 控件(View)之TextView, Button, ImageButton, ImageView, CheckBox, Ra
- 系出名门Android(5) – 控件(View)之TextView, Button, ImageButton, ImageView, CheckBox
- android:ImageView 和ImageButton的区别
- android中imageView属性src,background和scaleType之间的区别
- Android控件(View)之TextView, Button, ImageButton, ImageView, CheckBox 简单介绍
- 系出名门Android(5) - 控件(View)之TextView, Button, ImageButton, ImageView, CheckBox, RadioButton, AnalogClock, DigitalClock
- android:ImageView 和ImageButton的区别
- Android - 控件(View)之TextView, Button, ImageButton, ImageView, CheckBox, RadioButton, AnalogClock, DigitalClock 的简单介绍