我是靠谱客的博主 干净心锁,这篇文章主要介绍android xml实现按钮的圆角、阴影效果及按下变化效果的实现代码,现在分享给大家,希望可以做个参考。

前言

5个xml文件实现 按钮的圆角、阴影效果以及按下变化效果

实现

drawable/shape.xml

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 圆角 --> <corners android:radius="8dp" android:topLeftRadius="5dp" android:topRightRadius="5dp" android:bottomLeftRadius="5dp" android:bottomRightRadius="5dp"/><!-- 设置圆角半径 --> <!-- 间隔 --> <padding android:left="2dp" android:top="2dp" android:right="2dp" android:bottom="2dp"/><!-- 各方向的间隔 --> <!-- 大小 --> <size android:width="50dp" android:height="50dp"/><!-- 宽度和高度 --> <!-- 填充 --> <solid android:color=" #FF03A9F4"/><!-- 填充的颜色 --> </shape>

drawable/shapepress.xml

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 圆角 --> <corners android:radius="8dp" android:topLeftRadius="5dp" android:topRightRadius="5dp" android:bottomLeftRadius="5dp" android:bottomRightRadius="5dp"/><!-- 设置圆角半径 --> <!-- 间隔 --> <padding android:left="2dp" android:top="2dp" android:right="2dp" android:bottom="2dp"/><!-- 各方向的间隔 --> <!-- 大小 --> <size android:width="50dp" android:height="50dp"/><!-- 宽度和高度 --> <!-- 填充 --> <solid android:color="#FF7AD1F8"/><!-- 填充的颜色 --> </shape>

drawable/shadow.xml

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <!--第一层阴影--> <item> <shape android:shape="rectangle"> <solid android:color="#21000000" /> <corners android:radius="8dp" android:topLeftRadius="5dp" android:topRightRadius="5dp" android:bottomLeftRadius="5dp" android:bottomRightRadius="5dp" /> <!-- 大小 --> <size android:width="50dp" android:height="50dp"/><!-- 宽度和高度 --> </shape> </item> <!--第二层前景--> <item android:bottom="2dp" android:right="3dp" android:drawable="@drawable/shape" > </item> </layer-list>

drawable/shadowpress.xml

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <!--第一层阴影--> <item> <shape android:shape="rectangle"> <solid android:color="#0E000000" /> <corners android:radius="8dp" android:topLeftRadius="5dp" android:topRightRadius="5dp" android:bottomLeftRadius="5dp" android:bottomRightRadius="5dp" /> <!-- 大小 --> <size android:width="50dp" android:height="50dp"/><!-- 宽度和高度 --> </shape> </item> <!--第二层前景--> <!--第二层前景--> <item android:bottom="2dp" android:right="3dp" android:drawable="@drawable/shapepress" > </item> </layer-list>

drawable/button_style.xml

复制代码
1
2
3
4
5
6
7
8
<?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/shadowpress" /> <!-- 默认时的背景图片--> <item android:drawable="@drawable/shadow" /> </selector>

activity_main.xml中引入

复制代码
1
2
3
4
5
6
7
8
<Button android:id="@+id/button9" android:onClick="pushNumber" android:text="9" android:background="@drawable/button_style" android:textColor="@color/colorWhite" android:layout_margin="5dp" />

效果动态图

以上就是android xml实现按钮的圆角、阴影效果以及按下变化效果的详细内容,更多关于android按钮按下变化效果的资料请关注靠谱客其它相关文章!

最后

以上就是干净心锁最近收集整理的关于android xml实现按钮的圆角、阴影效果及按下变化效果的实现代码的全部内容,更多相关android内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(136)

评论列表共有 0 条评论

立即
投稿
返回
顶部