一、shape资源所在目录:
res/drawable/filename.xml
二、shape资源的引用
In Java: R.drawable.filename
In XML: @[package:]drawable/filename
<TextView
android:background="@drawable/gradient_box"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
-----------------
Resources res = getResources();
Drawable shape = res. getDrawable(R.drawable.gradient_box);
TextView tv = (TextView)findViewByID(R.id.textview);
tv.setBackground(shape);
三、常见标签的使用
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<!-- 给图形创造环绕边角,只有 android:shape="rectangle"才有效
android:radius="2dp"等效于==>( android:topLeftRadius="2dp",android:topRightRadius="2dp"
ndroid:bottomLeftRadius="2dp" android:bottomRightRadius="2dp")
-->
<corners
android:topLeftRadius="3dp"
android:topRightRadius="3dp"
android:bottomLeftRadius="2dp"
android:bottomRightRadius="2dp"/>
<!--给shape一个梯度颜色
android:startColor="#000000" 开始颜色
android:endColor=" #00CD00" 结束颜色
android:centerColor="#7D26CD" 中间颜色
android:type="sweep" 颜色梯度使用的类型,默认是linear
android:angle="0" 颜色梯度变化的角度,值必须是45的整数倍 ,否则此标签无效 0(默认)从左到右 90:从上到下
-->
<gradient
android:angle="90"
android:startColor="#FFA07A"
android:endColor="#7D26CD"/>
<!-- 设置View的内容的边距
<padding
android:left="60dp"
android:right="70dp"
android:top="10dp"
android:bottom="5dp"/>
-->
<!-- 设置shpae大小
<size
android:width="30dp"
android:height="37dp"/>
-->
<!-- 设置固定的颜色填充shape,这个颜色填满shpae空间
<solid
android:color="#B3EE3A"/>
-->
<!-- 分割条
<stroke
android:color="#FF00FF"
android:width="100dp"
android:dashGap="7dp"
android:dashWidth="7dp"/>
-->
</shape>