圆角边框和渐变

Demo2aasrcmain esdrawableg_border.xml

1 <?xml version="1.0" encoding="utf-8"?>
2 <shape xmlns:android="http://schemas.android.com/apk/res/android">
3     <!--设置背景色为透明色-->
4     <solid android:color="#0000"/>
5     <!--设置红色边框-->
6     <stroke android:width="4dp" android:color="#f00"/>
7 </shape>

Demo2aasrcmain esdrawableg_border2.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <shape xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:shape="rectangle">
 4     <!--指定圆角矩形的4个圆角的半径-->
 5     <corners
 6         android:bottomLeftRadius="5dp"
 7         android:bottomRightRadius="20dp"
 8         android:topLeftRadius="20dp"
 9         android:topRightRadius="5dp" />
10     <!--指定边框线条的宽度和颜色-->
11     <stroke
12         android:width="4dp"
13         android:color="#f0f" />
14     <!--指定使用渐变背景色,使用sweep类型的渐变(红绿蓝)-->
15     <gradient
16         android:centerColor="#0f0"
17         android:endColor="#00f"
18         android:startColor="#f00"
19         android:type="sweep" />
20 </shape>

Demo2aasrcmain eslayoutactivity_main.xml

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6     tools:context=".MainActivity">
 7 
 8     <!--通过background指定背景-->
 9     <TextView
10         android:layout_width="match_parent"
11         android:layout_height="wrap_content"
12         android:text="带边框的文本"
13         android:textSize="24sp"
14         android:background="@drawable/bg_border"/>
15     <TextView
16         android:layout_width="match_parent"
17         android:layout_height="wrap_content"
18         android:text="圆角边框,渐变背景的文本"
19         android:textSize="24sp"
20         android:background="@drawable/bg_border2"/>
21 
22 </LinearLayout>
原文地址:https://www.cnblogs.com/LY1124/p/4671980.html