Android Drawable Resources 之 Shape Drawable 使用详解
2017-09-07 20:20
531 查看
一、简介
Shape 是可以定义几何形状的XML文件,包括颜色和渐变等等。用作控件背景、填充色。文件位置:res/drawable/filename.xml
语 法:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape=["rectangle" | "oval" | "line" | "ring"] > <corners android:radius="integer" android:topLeftRadius="integer" android:topRightRadius="integer" android:bottomLeftRadius="integer" android:bottomRightRadius="integer" /> <gradient android:angle="integer" android:centerX="float" android:centerY="float" android:centerColor="integer" android:endColor="color" android:gradientRadius="integer" android:startColor="color" android:type=["linear" | "radial" | "sweep"] android:useLevel=["true" | "false"] /> <padding android:left="integer" android:top="integer" android:right="integer" android:bottom="integer" /> <size android:width="integer" android:height="integer" /> <solid android:color="color" /> <stroke android:width="integer" android:color="color" android:dashWidth="integer" android:dashGap="integer" /> </shape>
二、详解
1、shape 根属性(rectangle、oval、line、ring)android:shape=["rectangle" | "oval" | "line" | "ring"]
默认为矩形,可以设置为矩形(rectangle)、椭圆形(oval)、线性形状(line)、环形(ring)
2、corners 设置圆角,只有当 android:shape = "rectangle" 使用
<corners
android:radius="dimension" //全部的圆角半径
android:topLeftRadius="dimension" //左上角的圆角半径
android:topRightRadius="dimension" //右上角的圆角半径
android:bottomLeftRadius="dimension" //左下角的圆角半径
android:bottomRightRadius="dimension" /> //右下角的圆角半径
3、solid 设置内部填充色
<solid android:color="color"/>
4、gradient 设置渐变色,可以定义两色渐变和三色渐变,及渐变样式
<gradient
android:type=["linear" | "radial" | "sweep"] //线性渐变(默认)、放射渐变、扫描式渐变
android:angle="integer" //渐变角度,必须为45的倍数,0为从左到右,90为从上到下
android:centerX="float" //渐变中心X的相当位置,范围为0~1
android:centerY="float" //渐变中心Y的相当位置,范围为0~1
android:startColor="color" //渐变开始点的颜色
android:centerColor="color" //渐变中间点的颜色
android:endColor="color" //渐变结束点的颜色
android:gradientRadius="float" //渐变的半径,只有当渐变类型为radial时才能使用
android:useLevel=["true" | "false"] /> //使用LevelListDrawable时就要设置为true。设为false时才有渐变效果
5、stroke 设置描边属性,可以定义描边的宽度,颜色,虚实线等
<stroke
android:width="dimension" //描边的宽度
android:color="color" //描边的颜色
android:dashWidth="dimension" //虚线的宽度,值为0时是实线
android:dashGap="dimension" /> //虚线的间隔
6、size 用来设置图形的大小
<size
android:width="dimension"
android:height="dimension" />
7、padding 用来设置view内容距边界的距离
<padding
android:left="dimension"
android:top="dimension"
android:right="dimension"
android:bottom="dimension" />
三、示例
1、定义shape的XML文件gradient_box.xml<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <gradient android:startColor="#FFFF0000" android:endColor="#80FF00FF" android:angle="45"/> <padding android:left="7dp" android:top="7dp" android:right="7dp" android:bottom="7dp" /> <corners android:radius="8dp" /> </shape>
2、将这个XML文件添加到一个View
<TextView android:background="@drawable/gradient_box" android:layout_height="wrap_content" android:layout_width="wrap_content" />
3、使用java代码获取shape drawable,并且添加到View
Resources res = getResources(); Drawable shape = res. getDrawable(R.drawable.gradient_box); TextView tv = (TextView)findViewByID(R.id.textview); tv.setBackground(shape);
相关文章推荐
- Android 应用资源(三) Shape Drawable使用详解
- Android Drawable Resources 之 State List 使用详解
- Android Drawable - Shape Drawable使用详解(附图)
- Android Drawable - Shape Drawable使用详解(附图)
- 【Android UI】 Shape详解 (GradientDrawable)
- 【Android UI】 Shape详解 (GradientDrawable)
- Android中View绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解
- Android中View(视图)绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解
- Android Shape Drawable Resources TextView渐变 背景色 圆角
- 【Android UI】 Shape详解 (GradientDrawable)
- android UI之Shape详解_GradientDrawable
- Android自定义drawable(Shape)详解
- Android图形系统中形状Drawable使用详解
- 【Android UI】 Shape详解 (GradientDrawable)
- 【Android UI】 Shape详解 (GradientDrawable)
- android shape的使用详解以及常用效果(渐变色、分割线、边框、半透明阴影效果等)
- 【Android UI】 Shape详解 (GradientDrawable)
- Android Shape Drawable Resources
- Android中Shape Drawable在xml中的使用
- 【Android UI】 Shape详解 (GradientDrawable)