Android基本组件——1.Button、ImageView等的按下效果

本文介绍了如何在Android中为Button、ImageView等组件设置按下效果,主要通过XML中的selector实现,包括shape的使用如solid、stroke、corners、padding和gradient,并提及代码实现的两种方式。同时,文章提到了ImageView在设置点击效果时的注意事项。
摘要由CSDN通过智能技术生成

有关Button、ImageView、ImageButton、TextView等组件都可以设置它们的按下效果,有两种解决方案,一是在代码里面写,二是在xml中写,个人偏向于在xml写。


一:在xml中,可以用selector完成,例如:

一个Button平时状态:按下的状态:

drawable中新建一个xml文件:shape_common_radiusbtn,控制背景色

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version= "1.0"  encoding= "utf-8" ?>
<selector xmlns:android= "http://schemas.android.com/apk/res/android" >
 
     <item android:state_pressed= "true" >
         <shape>
             <corners android:radius= "5dp"  />
             <padding android:bottom= "1dp"  android:left= "1dp"  android:right= "1dp"  android:top= "1dp"  />
             <stroke android:width= "1dp"  android:color= "@color/text_brown_d7c093"  />
             <solid android:color= "@color/text_brown_d7c093"  />
         </shape></item>
 
     <item>
         <shape>
             <corners android:radius= "5dp"  />
             <padding android:bottom= "1dp"  android:left= "1dp"  android:right= "1dp"  android:top= "1dp"  />
             <stroke android:width= "1dp"  android:color= "@color/text_brown_d7c093"  />
             <solid android:color= "@color/white"  />
        
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值