加一个红色的边框:
textView的XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
>
<!-- 通过android:background指定背景 -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="带边框的文本"
android:textSize="24sp"
android:background="@drawable/bg_border"
/>
</LinearLayout>
边框XML:(新建文件夹drawable.然后在此文件夹下新建文件bg_border.xml)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 设置红色边框 -->
<stroke android:width="2dp" android:color="#f00"/>
</shape>
效果
渐变颜色:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<!-- 指定圆角矩形的4个圆角的半径 -->
<corners android:topLeftRadius="5dp"
android:topRightRadius="5dp"
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"
/>
<!-- 指定边框线条的宽度和颜色 -->
<stroke android:width="4dp" android:color="#f0f"/>
<!-- 指定使用渐变背景色,使用sweep类型的渐变,颜色从红色到绿色再到蓝色 -->
<gradient android:startColor="#f00"
android:centerColor="#0f0"
android:endColor="#00f"
android:angle="270"
android:centerX="0.5"
android:centerY="0.5"
/>
</shape>
效果:
说明:
(1)shape节点配置的是图形的形式,主要包括方形、圆形等
(2)gradient节点主要配置起点颜色、终点颜色及中间点的颜色、坐标、渐变效果(0,90,180从左到右渐变,270从上到下渐变)默认从左到右。
(3)corners节点配置四周圆角的半径。