Android之ImageButton、ImageView

来源:互联网 发布:淘宝女装简介范文 编辑:程序博客网 时间:2024/04/29 09:53

在Web开发中,HTML中对图片的操作就是提供一个<img>标签,我们通过该标签的src属性来制定图片资源的地址,从而在页面中显示一个图片。那么在Android中,ImageView就是用于图片显示的。 

ImageView的集成关系如下:

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 Demo"        android:src="@drawable/ic_launcher" />

代码非常的简单,ID就不多说了,然后设置长和宽,contentDescription是对图片的一个简要说明,最重要的src属性,这和HTML中的属性名称是一致的,也就是指定图片资源的位置。我们知道,Android项目中的图片资源统一位于res/drawable-xx文件夹内,不同的分辨率用于不同的设备,而我们只需通过@drawable/来调用图片资源即可。放置在drawable文件夹内的图片,会在R.java中自动注册,所以我们才能访问到其中的图片资源。 
 
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 Demo"        android:src="@drawable/ic_launcher" />


 

原创粉丝点击