从零开始学android<ImageView和ImageButton的使用.十.>

来源:互联网 发布:中科大还有网络教育吗 编辑:程序博客网 时间:2024/06/08 16:10
ImageView组件的主要功能是为图片展示提供一个容器,android.widget.ImageView类的定义如下:
java.lang.Object
   android.view.View
     android.widget.ImageView

常用方法
No.
配置属性名称
对应方法
描述
1
android:maxHeight
public void setMaxHeight (int maxHeight)
定义图片的最大高度
2
android:maxWidth
public void setMaxWidth (int maxWidth)
定义图片的最大宽度
3
android:src
public void setImageResource (int resId)
定义显示图片的ID
当然imageview里面还有许多设置图片的方法,大家可以自己在以后的开发中去看看
…………………………………………………………毫无美感的分割线…………………………………………………………
选择res下的drawable-hdip文件夹,将转备好的图片放入
然后定义 xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <ImageView        android:layout_width="wrap_content"//宽度包裹内容        android:layout_height="wrap_content"//高度包裹内容        android:src="@drawable/bg"//设置图片        /></RelativeLayout>

运行效果:

…………………………………………………………毫无美感的分割线…………………………………………………………
ImageButton和imageView基本类似,只不过ImageButton多了事件监听机制,和其他的一些操作
与按钮组件(Button)类似,在Android中又提供了一个图片按钮,可以直接使用ImageButton定义,此类定义如下:
java.lang.Object
   android.view.View
     android.widget.ImageView
       android.widget.ImageButton
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <ImageButton        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/abc"                /></RelativeLayout>


上图就是imageButton的显示效果

这节讲的比较简单,大家可以参照APi获得更多的方法。
下节预报:时间选择器:TimePicker
0 0
原创粉丝点击