界面组件——按钮(Button)与图片按钮(ImageButton)

介绍

Button继承了TextView,同时还是ImageButton的父类。

ButtonImageButton的功能主要是在UI界面上生成一个按钮,该按钮可以供用户单击,当用户单击时,按钮会触发一个OnClick事件

Button生成的按钮上显示文字,而ImageButton上则显示图片。即使为ImageButton指定android:text属性也不会在此按钮上显示任何文字。

可以通过android:backgroud为Button添加背景颜色或背景图片;通过android:img为ImageButton指定图片。


用例

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

    <!--指定按钮松开时的图片-->
    <item android:state_pressed="false"
        android:drawable="@drawable/leaf"/>
</selector>
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="horizontal">

    <TableRow>
        <!--普通文字按钮-->
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/bg_border"
            android:text="普通按钮"
            android:textSize="10pt"/>

        <!--普通图片按钮-->
        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/leaf"
            android:background="#000000"/>
    </TableRow>

    <TableRow>
        <!--按下时显示不同图片的按钮-->
        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/button_selector"
            android:background="#000000"/>

        <!--带文字的图片按钮-->
        <Button
            android:id="@+id/test"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/button_selector"
            android:text="带文字的图片按钮"/>
    </TableRow>

</TableLayout>

相应地界面展示结果:

图1


摘自《疯狂Android讲义》

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值