Android中shape属性详解和selector和layer-list的说明

一、简单使用

刚开始,就先不讲一堆标签的意义及用法,先简单看看shape标签怎么用。

1、新建shape文件

首先在res/drawable文件夹下,新建一个文件,命名为:shape_radius.xml

内容是这样的:(先不需要理解,先看shape怎么用)

 

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     <corners android:radius="20dip"/> 
  4.     <solid android:color="#ff00ff"/> 
  5.  
  6. </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、添加到控件中

在定义好shape文件后,下一步就是将其添加到控件中,添加到控件中,一般是使用设置background属性,将其为控件背景,下面,我们将其设置为MainActivity对应的布局中(activity_main.xml),将其设为TextView的背景,看显示出来 是什么样子的。

 

 

  1. <RelativeLayout 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.     tools:context="com.harvic.tryshape.MainActivity" 
  6.  
  7.     <TextView 
  8.         android:layout_width="wrap_content" 
  9.         android:layout_height="wrap_content" 
  10.         android:layout_margin="50dip" 
  11.         android:text="@string/hello_world"  
  12.         android:background="@drawable/shape_radius"/> 
  13.      
  14. </RelativeLayout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.harvic.tryshape.MainActivity" >

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

显示出来的结果是这样的:

 

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

上面给大家简单的讲了下shape标签组的简单使用方法,下面就具体讲讲shape标签里所具有的几个子标签及所具有的属性。

1、Corners

 

  1. <corners    //定义圆角   
  2.     android:radius="dimension"      //全部的圆角半径   
  3.     android:topLeftRadius="dimension"   //左上角的圆角半径   
  4.     android:topRightRadius="dimension"  //右上角的圆角半径   
  5.     android:bottomLeftRadius="dimension"    //左下角的圆角半径   
  6.     android:bottomRightRadius="dimension" />    //右下角的圆角半径   
<corners    //定义圆角  
    android:radius="dimension"      //全部的圆角半径  
    android:topLeftRadius="dimension"   //左上角的圆角半径  
    android:topRightRadius="dimension"  //右上角的圆角半径  
    android:bottomLeftRadius="dimension"    //左下角的圆角半径  
    android:bottomRightRadius="dimension" />    //右下角的圆角半径  

Corners标签是用来字义圆角的,其中radius与其它四个并不能共同使用。

 

android:radius:定义四个角的的圆角半径。

其它四个是逐个字义每个角的圆角半径。

使用:

控件布局:

 

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     android:layout_width="match_parent" 
  3.     android:layout_height="match_parent" 
  4.  
  5.     <TextView 
  6.         android:layout_width="100dp" 
  7.         android:layout_height="100dp" 
  8.         android:layout_margin="50dip" 
  9.         android:text="@string/hello_world"  
  10.         android:background="@drawable/shape_radius"/> 
  11. </RelativeLayout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_margin="50dip"
        android:text="@string/hello_world" 
        android:background="@drawable/shape_radius"/>
 </RelativeLayout>

shape定义:

 

 

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     <corners android:radius="20dip"/> 
  4.     <solid android:color="#ffff00"/> 
  5. </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="#ffff00"/>
</shape>

效果:

 

2、solid

solid用以指定内部填充色

只有一个属性:

 

  1. <solid  android:color="color" />   
<solid  android:color="color" />  

在上面的例子中,我们就将填充色指定为#ffff00了,如果我们不加圆角,只使用填充色,即将shape变成这样子:

 

 

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     <solid android:color="#ffff00"/> 
  4. </shape
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#ffff00"/>
</shape>

那效果就是这样的:

 

3、gradient

 

gradient用以定义渐变色,可以定义两色渐变和三色渐变,及渐变样式,它的属性有下面几个:

 

  1. <gradient  
  2.     android:type=["linear" | "radial" | "sweep"]    //共有3中渐变类型,线性渐变(默认)/放射渐变/扫描式渐变   
  3.     android:angle="integer"     //渐变角度,必须为45的倍数,0为从左到右,90为从上到下   
  4.     android:centerX="float"     //渐变中心X的相当位置,范围为0~1   
  5.     android:centerY="float"     //渐变中心Y的相当位置,范围为0~1   
  6.     android:startColor="color"   //渐变开始点的颜色   
  7.     android:centerColor="color"  //渐变中间点的颜色,在开始与结束点之间   
  8.     android:endColor="color"    //渐变结束点的颜色   
  9.     android:gradientRadius="float"  //渐变的半径,只有当渐变类型为radial时才能使用   
  10.     android:useLevel=["true" | "false"] />  //使用LevelListDrawable时就要设置为true。设为false时才有渐变效果   
<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时才有渐变效果  

首先有三种渐变类型,分别是:linear(线性渐变)、radial(放射性渐变)、sweep(扫描式渐变)

(1)先看看这几个属性的使用方法:

 

  1. android:type=["linear" | "radial" | "sweep"] 
  2. android:startColor="color"   //渐变开始点的颜色   
  3. android:centerColor="color"  //渐变中间点的颜色,在开始与结束点之间   
  4. android:endColor="color"    //渐变结束点的颜色   
  5. android:gradientRadius="float"  //渐变的半径,只有当渐变类型为radial时才能使用   
android:type=["linear" | "radial" | "sweep"]
android:startColor="color"   //渐变开始点的颜色  
android:centerColor="color"  //渐变中间点的颜色,在开始与结束点之间  
android:endColor="color"    //渐变结束点的颜色  
android:gradientRadius="float"  //渐变的半径,只有当渐变类型为radial时才能使用  

 

下面我们使用三色渐变来看看这三种渐变方式都是怎么显示的:(如果不使用centerColor属性就是双色渐变,这个属性是可选的)

需要注意的一点是,在构造放射性渐变时,要加上android:gradientRadius属性(渐变半径),即必须指定渐变半径的大小才会起作用,下面列出这三个渐变方式的shape代码,供大家参考:

线性渐变:

 

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     <gradient  
  4.         android:type="linear" 
  5.         android:startColor="#ff0000" 
  6.         android:centerColor="#00ff00" 
  7.         android:endColor="#0000ff"/> 
  8. </shape
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <gradient 
        android:type="linear"
        android:startColor="#ff0000"
        android:centerColor="#00ff00"
        android:endColor="#0000ff"/>
</shape>

放射性渐变:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     <gradient  
  4.         android:type="radial" 
  5.         android:startColor="#ff0000" 
  6.         android:centerColor="#00ff00" 
  7.         android:endColor="#0000ff" 
  8.         android:gradientRadius="100"/> 
  9. </shape
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <gradient 
        android:type="radial"
        android:startColor="#ff0000"
        android:centerColor="#00ff00"
        android:endColor="#0000ff"
        android:gradientRadius="100"/>
</shape>

扫描式渐变:

 

 

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     <gradient  
  4.         android:type="sweep" 
  5.         android:startColor="#ff0000" 
  6.         android:centerColor="#00ff00" 
  7.         android:endColor="#0000ff"/> 
  8. </shape
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <gradient 
        android:type="sweep"
        android:startColor="#ff0000"
        android:centerColor="#00ff00"
        android:endColor="#0000ff"/>
</shape>

可见放射性渐变,只是比其它两个多了个android:gradientRadius属性

(2)、android:angle属性(仅对线性渐变有效)

 

  1. android:angle="integer"     //渐变角度,必须为45的倍数,0为从左到右,90为从上到下   
android:angle="integer"     //渐变角度,必须为45的倍数,0为从左到右,90为从上到下  

我们在上面的三种渐变上都加上angle属性,看看效果如何:

能过跟上一个图对比可以发现,angle属性确实只对线性渐变有效,其它两种渐变方式都没有任何动静,下面是此时的线性渐变shape代码:

 

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     <gradient  
  4.         android:type="linear" 
  5.         android:startColor="#ff0000" 
  6.         android:centerColor="#00ff00" 
  7.         android:endColor="#0000ff" 
  8.         android:angle="45"/> 
  9. </shape
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <gradient 
        android:type="linear"
        android:startColor="#ff0000"
        android:centerColor="#00ff00"
        android:endColor="#0000ff"
        android:angle="45"/>
</shape>
(3)、android:centerX与android:centerY

centerX、centerY两个属性用于设置渐变的中心点位置,仅当渐变类型为放射渐变时有效,类型为分数或小数,不接受Dimension。默认值是0.5,有效值是0.0~1.0,超出该范围后会看不出渐变效果。centerX、centerY的取值其实是宽和高的百分比;不难理解,下面看代码:

 

 

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     <gradient  
  4.         android:type="sweep" 
  5.         android:startColor="#ff0000" 
  6.         android:centerColor="#00ff00" 
  7.         android:endColor="#0000ff" 
  8.         android:centerX="0.2" 
  9.         android:centerY="0.8"/> 
  10. </shape
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <gradient 
        android:type="sweep"
        android:startColor="#ff0000"
        android:centerColor="#00ff00"
        android:endColor="#0000ff"
        android:centerX="0.2"
        android:centerY="0.8"/>
</shape>

取宽度的20%和高度的80%的位置,作为新的渐变原点,效果是这样的:

 

(4)android:useLevel

useLevel属性通常不使用。该属性用于指定是否将该shape当成一个LevelListDrawable来使用,默认值为false。

4、stroke

这是描边属性,可以定义描边的宽度,颜色,虚实线等

  1. <stroke        
  2.     android:width="dimension"   //描边的宽度   
  3.     android:color="color"   //描边的颜色   
  4.     // 以下两个属性设置虚线   
  5.     android:dashWidth="dimension"   //虚线的宽度,值为0时是实线   
  6.     android:dashGap="dimension" />      //虚线的间隔  
<stroke       
    android:width="dimension"   //描边的宽度  
    android:color="color"   //描边的颜色  
    // 以下两个属性设置虚线  
    android:dashWidth="dimension"   //虚线的宽度,值为0时是实线  
    android:dashGap="dimension" />      //虚线的间隔 

 

上面各个属性的意义如下:

我们使用绿色虚线描边,虚线高度是20dp,虚线宽度为10dp虚线间距为1dp:

 

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     <stroke  
  4.         android:width="20dp"  
  5.         android:color="#00ff00" 
  6.         android:dashWidth="10dp" 
  7.         android:dashGap="1dp" /> 
  8. </shape
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <stroke 
        android:width="20dp" 
        android:color="#00ff00"
        android:dashWidth="10dp"
        android:dashGap="1dp" />
</shape>

从效果图中,我们也能清晰的看出这三个参数(width、dashwidth、dashGap)之间的区别: 

 

5、size和padding

这两个基本上不怎么用,因为他们所具有的功能,控件本身也能实现。 size:是用来定义图形的大小的。

 

  1. <size   
  2.     android:width="dimension"   
  3.     android:height="dimension" /> 
<size  
    android:width="dimension"  
    android:height="dimension" />

padding:用来定义内部边距

  1. <padding    
  2.     android:left="dimension"   
  3.     android:top="dimension"   
  4.     android:right="dimension"   
  5.     android:bottom="dimension" /> 
<padding   
    android:left="dimension"  
    android:top="dimension"  
    android:right="dimension"  
    android:bottom="dimension" />

三、Shape的属性(rectangle、oval、line、ring)

上面我们讲了Shape的子标签的的作用,但Shape本身还没讲,Shape自已是可以定义当前Shape的形状的,比如上面的矩形,还有椭圆形,线形和环形;这些都是通过Shape标签的 shape属性来定义的,Shape标签总共有下面几个属性,我们一个个讲:

 

  1. android:shape=["rectangle" | "oval" | "line" | "ring"]   
  2. shape的形状,默认为矩形,可以设置为矩形(rectangle)、椭圆形(oval)、线性形状(line)、环形(ring)   
  3. 下面的属性只有在android:shape="ring时可用:   
  4. android:innerRadius         尺寸,内环的半径。   
  5. android:innerRadiusRatio    浮点型,以环的宽度比率来表示内环的半径,   
  6. android:thickness           尺寸,环的厚度   
  7. android:thicknessRatio      浮点型,以环的宽度比率来表示环的厚度,例如,如果android:thicknessRatio="2",   
  8. android:useLevel            boolean值,如果当做是LevelListDrawable使用时值为true,否则为false.  
android:shape=["rectangle" | "oval" | "line" | "ring"]  
shape的形状,默认为矩形,可以设置为矩形(rectangle)、椭圆形(oval)、线性形状(line)、环形(ring)  
下面的属性只有在android:shape="ring时可用:  
android:innerRadius         尺寸,内环的半径。  
android:innerRadiusRatio    浮点型,以环的宽度比率来表示内环的半径,  
android:thickness           尺寸,环的厚度  
android:thicknessRatio      浮点型,以环的宽度比率来表示环的厚度,例如,如果android:thicknessRatio="2",  
android:useLevel            boolean值,如果当做是LevelListDrawable使用时值为true,否则为false. 

可见,只有第一个shape是可用的,其它五个都是shape等于ring时所特有的。

 

注意,无论这里shape取什么形状,他的子标签都是可用的,但有时并不会有效果,比如在shape为椭圆时,那corners标签就不会有效果,很显然的道理。下面一个个看看各个形状都是怎么样的;

1、rectangle (矩形)

这就是上一节我们使用的形状,当我们不指定shape属性时,默认就是矩形的。

控件代码:

 

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     android:layout_width="match_parent" 
  3.     android:layout_height="match_parent" 
  4.     android:orientation="horizontal" >     
  5.     <TextView 
  6.         android:layout_width="300dp" 
  7.         android:layout_height="100dp" 
  8.         android:layout_margin="10dp" 
  9.         android:textColor="#ffffff" 
  10.         android:textSize="18sp" 
  11.         android:textStyle="bold" 
  12.         android:background="@drawable/try_shape_3"/> 
  13. </LinearLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >    
    <TextView
        android:layout_width="300dp"
        android:layout_height="100dp"
        android:layout_margin="10dp"
        android:textColor="#ffffff"
        android:textSize="18sp"
        android:textStyle="bold"
        android:background="@drawable/try_shape_3"/>
 </LinearLayout>

 

shape代码:

 

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:shape="rectangle"
  4.     <solid android:color="#ff00ff"/> 
  5. </shape
<?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>

对应图形: 

 

2、oval(椭圆)

控件代码不变,下面是shape代码:

 

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:shape="oval"
  4.     <solid android:color="#ff00ff"/> 
  5. </shape
<?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>

对应图形:(控件大小的矩形所对应的椭圆)

 

3、line(线形)

 

没觉得这个能有什么用……,也不讲了,没什么意思

4、ring(环形)

还记得他所特有的几个属性么:

 

  1. android:innerRadius         尺寸,内环的半径。   
  2. android:thickness           尺寸,环的厚度   
  3. android:innerRadiusRatio    浮点型,以环的宽度比率来表示内环的半径,   
  4.       例如,如果android:innerRadiusRatio,表示内环半径等于环的宽度除以5,这个值是可以被覆盖的,默认为9.   
  5. android:thicknessRatio      浮点型,以环的宽度比率来表示环的厚度,例如,如果android:thicknessRatio="2",   
  6.       那么环的厚度就等于环的宽度除以2。这个值是可以被android:thickness覆盖的,默认值是3.   
  7. android:useLevel            boolean值,如果当做是LevelListDrawable使用时值为true,否则为false. 
android:innerRadius         尺寸,内环的半径。  
android:thickness           尺寸,环的厚度  
android:innerRadiusRatio    浮点型,以环的宽度比率来表示内环的半径,  
      例如,如果android:innerRadiusRatio,表示内环半径等于环的宽度除以5,这个值是可以被覆盖的,默认为9.  
android:thicknessRatio      浮点型,以环的宽度比率来表示环的厚度,例如,如果android:thicknessRatio="2",  
      那么环的厚度就等于环的宽度除以2。这个值是可以被android:thickness覆盖的,默认值是3.  
android:useLevel            boolean值,如果当做是LevelListDrawable使用时值为true,否则为false.

这么几个属性无外乎就是定义环形的内环尺寸和环的宽度。

 

举个例子:

控件定义:

 

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     android:layout_width="match_parent" 
  3.     android:layout_height="match_parent" 
  4.     android:orientation="horizontal" >     
  5.     <TextView 
  6.         android:layout_width="300dp" 
  7.         android:layout_height="100dp" 
  8.         android:layout_margin="10dp" 
  9.         android:textColor="#ffffff" 
  10.         android:textSize="18sp" 
  11.         android:textStyle="bold" 
  12.         android:background="@drawable/try_shape_2"/> 
  13. </LinearLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >    
    <TextView
        android:layout_width="300dp"
        android:layout_height="100dp"
        android:layout_margin="10dp"
        android:textColor="#ffffff"
        android:textSize="18sp"
        android:textStyle="bold"
        android:background="@drawable/try_shape_2"/>
 </LinearLayout>

shape定义:(这里一定要要加上useLevel属性并定义为false,不然没有效果)

 

 

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:shape="ring"  
  4.     android:innerRadius="20dp"  
  5.     android:thickness="50dp"   
  6.     android:useLevel="false"
  7.      
  8.     <solid android:color="#ff00ff"/> 
  9.      
  10. </shape
<?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的使用最多的还是第二部分,即当shape为矩形时,结合那几个子标签共同使用时,对于其它的三个Shape平时用到的可能性不大,我这里讲的也不太细致,下面给大家第一部分的源码,直接运行,就能出来一个Shape效果。

 

参考文章:

1、《Android中shape的使用》

2、《Android中shape的使用》(与第一个标题一样而已)

3、《shape 详解》

4、《Android中shape的用法详解》

5、《Android shape文件属性详解》

源码地址:http://download.csdn.net/detail/harvic880925/8249629


/************************************************************/


<shape>和<selector>在Android UI设计中经常用到。比如我们要自定义一个圆角Button,点击Button有些效果的变化,就要用到<shape>和<selector>。
可以这样说,<shape>和<selector>在美化控件中的作用是至关重要。

在看这篇文章之前,可以看下这个小例子:镂空按钮的实现

1.Shape
简介

作用:XML中定义的几何形状

位置:res/drawable/文件的名称.xml

使用的方法:

Java代码中:R.drawable.文件的名称

XML中:Android:background="@drawable/文件的名称"

属性:

<shape>  Android:shape=["rectangle" | "oval" | "line" | "ring"]

其中rectagle矩形,oval椭圆,line水平直线,ring环形

<shape>中子节点的常用属性:

<gradient>  渐变

Android:startColor  

起始颜色

Android:endColor  

结束颜色             

Android:angle  

渐变角度,0从左到右,90表示从下到上,数值为45的整数倍,默认为0;

Android:type  

渐变的样式 liner线性渐变 radial环形渐变 sweep

<solid >  内部填充

Android:color  

填充的颜色

<stroke >描边

Android:width 

描边的宽度

Android:color 

描边的颜色

Android:dashWidth

 表示'-'横线的宽度

Android:dashGap 

表示'-'横线之间的距离

<corners >圆角

Android:radius  

圆角的半径 值越大角越圆

Android:topRightRadius  

右上圆角半径

Android:bottomLeftRadius 

右下圆角角半径

Android:topLeftRadius 

左上圆角半径

Android:bottomRightRadius 

左下圆角半径

<padding >边界填充

android:bottom="1.0dip" 

底部填充

android:left="1.0dip" 

左边填充

android:right="1.0dip" 

右边填充

android:top="0.0dip" 

上面填充

2.Selector
简介

根据不同的选定状态来定义不同的现实效果

分为四大属性:

android:state_selected 是否选中

android:state_focused 是否获得焦点

android:state_pressed 是否按压

android:state_enabled 是否设置是否响应事件,指所有事件

另:

android:state_window_focused 默认时的背景图片

引用位置:res/drawable/文件的名称.xml

使用的方法:
Java代码中:R.drawable.文件的名称
XML中:Android:background="@drawable/文件的名称"
[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8" ?>       
  2. <selector xmlns:Android="http://schemas.android.com/apk/res/android">     
  3. <!-- 默认时的背景图片-->      
  4. <item Android:drawable="@drawable/pic1" />        
  5. <!-- 没有焦点时的背景图片 -->      
  6. <item   
  7.    Android:state_window_focused="false"        
  8.    android:drawable="@drawable/pic_blue"   
  9.    />       
  10. <!-- 非触摸模式下获得焦点并单击时的背景图片 -->      
  11. <item   
  12.    Android:state_focused="true"   
  13.    android:state_pressed="true"     
  14.    android:drawable"@drawable/pic_red"   
  15.    />     
  16. <!-- 触摸模式下单击时的背景图片-->      
  17. <item   
  18.    Android:state_focused="false"   
  19.    Android:state_pressed="true"     
  20.    Android:drawable="@drawable/pic_pink"   
  21.    />      
  22. <!--选中时的图片背景-->      
  23. <item   
  24.    Android:state_selected="true"   
  25.    android:drawable="@drawable/pic_orange"   
  26.    />       
  27. <!--获得焦点时的图片背景-->      
  28. <item   
  29.    Android:state_focused="true"   
  30.    Android:drawable="@drawable/pic_green"   
  31.    />       
  32. </selector>   


3.layer-list   
简介:
将多个图片或上面两种效果按照顺序层叠起来
例子:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <layer-list xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <item>  
  4.       <bitmap android:src="@drawable/android_red"  
  5.         android:gravity="center" />  
  6.     </item>  
  7.     <item android:top="10dp" android:left="10dp">  
  8.       <bitmap android:src="@drawable/android_green"  
  9.         android:gravity="center" />  
  10.     </item>  
  11.     <item android:top="20dp" android:left="20dp">  
  12.       <bitmap android:src="@drawable/android_blue"  
  13.         android:gravity="center" />  
  14.     </item>  
  15. </layer-list>  
[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <ImageView  
  2.     android:layout_height="wrap_content"  
  3.     android:layout_width="wrap_content"  
  4.     android:src="@drawable/layers" />  


效果图:



4.最后

以上三个标签可以揉合到一块儿来使用,比如下面这个例子:


[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  2.     <item android:state_pressed="true">  
  3.         <layer-list>  
  4.             <item android:bottom="8.0dip">  
  5.                 <shape>  
  6.                     <solid android:color="#ffaaaaaa" />  
  7.                 </shape>  
  8.             </item>  
  9.             <item>  
  10.                 <shape>  
  11.                     <corners android:bottomLeftRadius="4.0dip" android:bottomRightRadius="4.0dip" android:topLeftRadius="1.0dip" android:topRightRadius="1.0dip" />  
  12.   
  13.                     <solid android:color="#ffaaaaaa" />  
  14.   
  15.                     <padding android:bottom="1.0dip" android:left="1.0dip" android:right="1.0dip" android:top="0.0dip" />  
  16.                 </shape>  
  17.             </item>  
  18.             <item>  
  19.                 <shape>  
  20.                     <corners android:bottomLeftRadius="3.0dip" android:bottomRightRadius="3.0dip" android:topLeftRadius="1.0dip" android:topRightRadius="1.0dip" />  
  21.   
  22.                     <solid android:color="@color/setting_item_bgcolor_press" />  
  23.                 </shape>  
  24.             </item>  
  25.         </layer-list>  
  26.     </item>  
  27.     <item>  
  28.         <layer-list>  
  29.             <item android:bottom="8.0dip">  
  30.                 <shape>  
  31.                     <solid android:color="#ffaaaaaa" />  
  32.                 </shape>  
  33.             </item>  
  34.             <item>  
  35.                 <shape>  
  36.                     <corners android:bottomLeftRadius="4.0dip" android:bottomRightRadius="4.0dip" android:topLeftRadius="1.0dip" android:topRightRadius="1.0dip" />  
  37.   
  38.                     <solid android:color="#ffaaaaaa" />  
  39.   
  40.                     <padding android:bottom="1.0dip" android:left="1.0dip" android:right="1.0dip" android:top="0.0dip" />  
  41.                 </shape>  
  42.             </item>  
  43.             <item>  
  44.                 <shape>  
  45.                     <corners android:bottomLeftRadius="3.0dip" android:bottomRightRadius="3.0dip" android:topLeftRadius="1.0dip" android:topRightRadius="1.0dip" />  
  46.   
  47.                     <solid android:color="@color/setting_item_bgcolor" />  
  48.                 </shape>  
  49.             </item>  
  50.         </layer-list>  
  51.     </item>  
  52. </selector>  


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值