•参考资料
•常用属性
- gradient........颜色渐变
- startcolor......起点颜色
- endcolor.......终点颜色
- solid.............填充
- stroke...........描边
- corners.........圆角
•Code及效果图片
1.颜色渐变1 <?xml version="1.0" encoding="utf-8"?> 2 <shape xmlns:android="http://schemas.android.com/apk/res/android" 3 android:shape="rectangle"> 4 5 <gradient 6 android:startColor="#9C27B0" 7 android:endColor="#2196F3"/> 8 9 </shape>
2.背景填充成蓝色1 <?xml version="1.0" encoding="utf-8"?> 2 <shape xmlns:android="http://schemas.android.com/apk/res/android" 3 android:shape="rectangle"> 4 5 <solid 6 android:color="#2196F3"/> 7 8 </shape>
3.白底色红描边1 <?xml version="1.0" encoding="utf-8"?> 2 <shape xmlns:android="http://schemas.android.com/apk/res/android" 3 android:shape="rectangle"> 4 5 <solid 6 android:color="#FCFCFC"/> 7 <stroke 8 android:width="1dp" 9 android:color="#F44336"/> 10 11 </shape>
4.圆边1 <?xml version="1.0" encoding="utf-8"?> 2 <shape xmlns:android="http://schemas.android.com/apk/res/android" 3 android:shape="rectangle"> 4 5 <size 6 android:width="100dp" 7 android:height="200dp"/> 8 <solid 9 android:color="#2196F3"/> 10 <corners 11 android:radius="10dp"/> 12 13 </shape>
![]()
![]()
![]()
(1) (2) (3) (4)
•按下按钮时出现颜色变化
首先,新建一个 drawable resource file,并使用 selector ;
由蓝变为紫1 <?xml version="1.0" encoding="utf-8"?> 2 <selector xmlns:android="http://schemas.android.com/apk/res/android"> 3 4 <item android:state_pressed="false"> 5 <shape> 6 <solid android:color="#2196F3"/> 7 <corners android:radius="10dp"/> 8 </shape> 9 </item> 10 11 <item android:state_pressed="true"> 12 <shape> 13 <solid android:color="#9C27B0"/> 14 <corners android:radius="10dp"/> 15 </shape> 16 </item> 17 18 </selector>效果演示:
原文:https://www.cnblogs.com/hyacinthLJP/p/12317715.html