更新时间:2022-09-04 17:54:48
1.线line:
1
2
3
4
5
6
7
8
|
<?xml version= "1.0" encoding= "utf-8" ?>
<shape xmlns:android= "http://schemas.android.com/apk/res/android"
android:shape= "line" >
<stroke android:width= "8dp"
android:color= "#ff00ff00"
android:dashWidth= "6dp"
android:dashGap= "2dp" />
</shape> |
2.椭圆oval:
1
2
3
4
5
6
|
<?xml version= "1.0" encoding= "utf-8" ?>
<shape xmlns:android= "http://schemas.android.com/apk/res/android"
android:shape= "oval" >
<solid android:color= "#ff345678" />
<stroke android:width= "20dp" android:color= "#ff876543" />
</shape> |
3.长方形rectangle:
1
2
3
4
5
6
7
8
9
10
11
|
<?xml version= "1.0" encoding= "utf-8" ?>
<shape xmlns:android= "http://schemas.android.com/apk/res/android"
android:shape= "rectangle" >
<size android:width= "200dp"
android:height= "300dp" />
<solid android:color= "#ff665544" />
<stroke android:width= "20dp"
android:color= "#445566"
android:dashWidth= "40dp"
android:dashGap= "5dp" />
</shape> |
4.填充颜色solid、渐变gradient、边角corners:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<?xml version= "1.0" encoding= "utf-8" ?>
<shape xmlns:android= "http://schemas.android.com/apk/res/android"
android:shape= "rectangle" >
<size android:width= "200dp"
android:height= "300dp" />
<solid android:color= "#ff665544" />
<stroke android:width= "20dp"
android:color= "#445566"
android:dashWidth= "40dp"
android:dashGap= "5dp" />
<gradient android:startColor= "#ff5533aa"
android:centerColor= "#ff996633"
android:endColor= "#ff114477"
android:angle= "135"
/>
<corners
android:topLeftRadius= "45dp"
android:topRightRadius= "45dp"
android:bottomLeftRadius= "135dp"
android:bottomRightRadius= "135dp" />
<!-- android:radius= "45dp" -->
<!-- 渐变角度 只能被 45 整除 -->
</shape> |
5.代码编写
1
2
3
4
5
6
7
8
9
|
//白色背景shape int strokeWidth = 5 ; // 3dp
int roundRadius = 15 ; // 8dp
int strokeColor = Color.parseColor( "#2E3135" );
int fillColor = Color.parseColor( "#DFDFE0" );
GradientDrawable gd = new GradientDrawable();
gd.setColor(fillColor); gd.setCornerRadius(roundRadius); gd.setStroke(strokeWidth, strokeColor); |