Android基本组件(三)

536 查看

第三组UI组件:ImageView及其子类

1.ImageView继承View组件,不单单用于显示图片,用 XML代码 编写的Drawable也可以显示出来。
其中的XML属性 android:scaleType(设置图片如何缩放或移动以适应ImageView的大小) 有很多的属性值,如:matrix(使用矩形方式进行缩放)fitXY(对图片横向`纵向缩放)center(图片放在ImageView中间)等等...
下面是XML代码:

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_launcher"
        android:layout_toRightOf="@+id/textView1" />

2.ImageButton,与前面讲的Button的区别在于Button生成的按钮上显示文字,而ImageButton显示图片,对它设置android:text属性是没用的。
下面是XML代码:使用android:src 自定义Drawable对象

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/button_selector" />

补充:drawable的XML文件在前面的笔记(二)也有写过Android基本组件(笔记二)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<!--     指定按钮按下时的图片 -->
    <item android:state_pressed="true"
        android:drawable="@drawable|d1"/>
<!--     指定按钮松开时的图片 -->
    <item android:state_pressed="false"
        android:drawable="drawable|d2"/>

</selector>

3.ZoomButton代表“放大”,“缩小”两个按钮,android默认提供了btn_minus,btn_plus两个drawable资源。

    <ZoomButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn_zoom_down"
        android:src="@android:drawable/btn_minus" />
    <ZoomButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn_zoom_up"
        android:src="@android:drawable/btn_plus" />

4.QuickContactBadge,继承了ImageView,可通过android src 添加图片。增加的额外功能是该图片可以关联到手机中指定的联系人,当用户点击该图片时系统将会打开相应联系人的联系方式界面。

<QuickContactBadge
    android:id="@+id/badge"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:src="@drawable/ic_launcher"/>

补充: Activity下的Java代码,单击上面定义的src的ic_launcher图片,就可以连接到020-88888888

        // 获取QuickContactBadge组件
        badge = (QuickContactBadge) findViewById(R.id.badge);
        // 将QuickContactBadge组件与特定电话号码对应的联系人建立关联
        badge.assignContactFromPhone("020-88888888", false);