ImageView和ImageButton
来源:互联网 发布:外观模式 java代码 编辑:程序博客网 时间:2024/05/28 01:34
在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" />
<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中自动注册,所以我们才能访问到其中的图片资源。
这个程序很简单,我们直接运行代码看一下效果:
对于ImageView没有什么可以多说的内容,下面直接来看看ImageButton。
ImageButton就是图片按钮,在HTML中,我们定义按钮可以通过<button>标签来定义,表单中的按钮还可以是<submit>和<reset>,但是原生的HTML对按钮没有任何装饰,显示效果不好,我们可以通过CSS来美化按钮,比如加上图片。而在Android中,有ImageButton组件,可以为我们直接达到此目的。
首先,来看一下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" />
<ImageButton android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="ImageButton Demo" android:src="@drawable/ic_launcher" />
只是修改了一下标签名称,其余内容都不需要变化,那么再次运行程序,我们可以看到如下效果:
很容易看出,就是在按钮上加了图片的效果,只是这里我们的图片比较小,按钮部分还留有显示,只要做出合适大小的按钮图片,那么ImageButton就全是图片显示了,只不过这个图片可以进行点击操作。关于ImageButton也没有需要多说的了,需要时会用就行了。
- ImageView和ImageButton
- ImageButton和ImageView
- ImageView和ImageButton
- imageview和imagebutton
- ImageView和ImageButton和Button
- Android中ImageView和ImageButton
- UI控件--ImageView和ImageButton
- ImageButton和ImageView的使用
- ImageView和ImageButton的区别
- Android中的ImageView和ImageButton
- android:ImageView 和ImageButton的区别
- 图像按钮ImageButton和图像ImageView
- android组件ImageView和ImageButton无法正常使用
- 从零开始学android<ImageView和ImageButton的使用.十.>
- Android学习笔记十:基本视图组件:ImageView和ImageButton
- Android UI设计——ImageView和ImageButton控件
- Android UI 设计——ImageView 和 ImageButton 控件
- Android中ImageView和ImageButton的比较详解
- 最小割集 hdu 3002 ( 模板)
- grep比较两个文件的异同
- size_type 的陷阱
- django的安装及web部署
- struts2 标签截取字符串 struts2-在jsp页面中控制只显示前n个字符
- ImageView和ImageButton
- 程序题:将字串“变电站,nokv,变压器,高压输出”分割,添加到list中
- ubuntu常用命令记录
- 学习笔记IV——2012 Microsoft Intern Hiring Written Test (2012微软实习生招聘笔试题)
- Spring事务配置的五种方式
- 以下命令能将当前目录下的所有目录打包成tar.gz文件
- 项目四
- vs2008突然不能使用查找功能了
- MySql常用命令总结