Android控件笔记——按钮

Button和ImageButton的特征

1、共有的特征:

    都可以作为一个按钮产生点击事件;

2、不同点:

    Button有Text属性,ImageButton没有;

    ImageButton有src属性,Button没有。

3、产生明显的点击效果

 

Button:

一般name等的字符串我们不会直接写在XML文件中,而是写在res/values文件夹下的strings.xml文件中。

<string name="button_name">登录</string>

然后在activity_main.xml中如下代码中最后一句一样设置:

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/button_name" />

ImageButton:

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_alignRight="@+id/button1"
        android:layout_below="@+id/button1"
        android:src="@drawable/ic_launcher" />

则可实现以下效果:

Android控件笔记——按钮_第1张图片

 

注意:设置background图片会自动填充整个ImageButton,设置src图片会自适应

你可能感兴趣的:(Android控件笔记——按钮)