shape的使用创建步骤文件要放在drawable文件夹下如果放在layout下会报错

来源:互联网 发布:linux 安装rpm命令 编辑:程序博客网 时间:2024/05/19 07:09

shape的xml资源文件要放在drawable文件夹下如果放在layout下会报错

**

shape的创建和使用步骤

**
1.创建shape.xml文件放在drawable文件夹下,这里定义shape图形样式

    <?xml version="1.0" encoding="utf-8"?>     <shape xmlns:android="http://schemas.android.com/apk/res/android" >         <corners android:radius="20dip"/>         <solid android:color="#ff00ff"/>     </shape> 

2.添加到控件中,一般作为background属性引用

   <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_margin="50dip"         android:text="@string/hello_world"          android:background="@drawable/shape"/>  

运行之后就可以看到效果了,就是自己定义了一个图形,然后作为控件的背景显示一下而已,就和一张图片作为背景效果是一样的,只不过这里我们的形状是通过代码自己写的。

**

Shape自身的属性(rectangle、oval、line、ring)

rectangle (矩形)

    <?xml version="1.0" encoding="utf-8"?>     <shape xmlns:android="http://schemas.android.com/apk/res/android"          android:shape="rectangle">         <solid android:color="#ff00ff"/>     </shape> 

oval(椭圆)

    <?xml version="1.0" encoding="utf-8"?>     <shape xmlns:android="http://schemas.android.com/apk/res/android"          android:shape="oval">         <solid android:color="#ff00ff"/>     </shape> 

line(线形)

 <?xml version="1.0" encoding="utf-8"?>     <shape xmlns:android="http://schemas.android.com/apk/res/android"          android:shape="line">         <solid android:color="#ff00ff"/>     </shape> 

ring(环形)
他的一些属性

    android:innerRadius         尺寸,内环的半径。       android:thickness           尺寸,环的厚度       android:innerRadiusRatio    浮点型,以环的宽度比率来表示内环的半径,   
    <?xml version="1.0" encoding="utf-8"?>     <shape xmlns:android="http://schemas.android.com/apk/res/android"          android:shape="ring"          android:innerRadius="20dp"          android:thickness="50dp"           android:useLevel="false">         <solid android:color="#ff00ff"/>     </shape> 

**

shape的子标签属性,可以通过这些属性定义图形的形状,颜色,渐变色,描边

基本属性(corners、gradient、padding、size、solid、stroke)

corners定义圆角形状

    <corners    //定义圆角           android:radius="dimension"      //全部的圆角半径           android:topLeftRadius="dimension"   //左上角的圆角半径           android:topRightRadius="dimension"  //右上角的圆角半径           android:bottomLeftRadius="dimension"    //左下角的圆角半径           android:bottomRightRadius="dimension" />    //右下角的圆角半径   

solid 定义图形填充的颜色

    <?xml version="1.0" encoding="utf-8"?>     <shape xmlns:android="http://schemas.android.com/apk/res/android" >         <solid android:color="这里设置你想要的颜色"/>     </shape> 

gradient 定义图形的颜色渐变效果,可以定义两种颜色渐变 或者 三种颜色渐变

    <gradient          android:type=["linear" | "radial" | "sweep"]    //共有3中渐变类型,线性渐变(默认)/放射渐变/扫描式渐变           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时才有渐变效果   

stroke 给图形描边属性,可以定义描边的宽度,颜色,虚实线等

    <stroke                android:width="dimension"   //描边的宽度           android:color="color"   //描边的颜色           // 以下两个属性设置虚线           android:dashWidth="dimension"   //虚线的宽度,值为0时是实线           android:dashGap="dimension" />      //虚线的间隔  
1 0
原创粉丝点击