Android:Selector与Shape、layer-list

1. Selector


引用位置:res/drawable/文件的名称.xml
Java代码中:R.drawable.文件的名称
XML中:Android:background="@drawable/文件的名称"
 

item 中可以有以下属性:

[html]  view plain copy print ?
  1. android:drawable ="@drawable/drawable_resource"  
  2. android:state_pressed        =["true"| "false"] 点击   
  3. android:state_focused        =["true"| "false"] 获得焦点   
  4. android:state_selected       =["true"| "false"] 选中   
  5. android:state_active         =["true"| "false"]    
  6. android:state_checkable      =["true"| "false"] 是否可选择   
  7. android:state_checked        =["true"| "false"] 选择   
  8. android:state_enabled        =["true"| "false"] 是否响应事件,指所有事件   
  9. android:state_window_focused =["true"| "false"] 默认时的背景图片 


例:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" android:state_enabled="true"
        android:drawable="@drawable/shape_topcorner_pressed"/>
  
    <item android:drawable="@drawable/shape_topcorner_normal"/>
</selector>
注:<item>的选择类似于switch case语句

2. Shape


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

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

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

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


 属性:

    shape=["rectangle" | "oval" | "line" | "ring"]       其中rectangle为矩形,oval为椭圆,line为水平直线,ring为环形


        solid:实心,就是填充     

android:color =  "#000000"      指定填充的颜色


            gradient:渐变

[html]  view plain copy print ?
  1. android:startColor  起始颜色   
  2. android:endColor    结束颜色   
  3. android:angle       渐变角度,0表示从上到下,90表示从左到右边,数值必须为45的整数倍,默认为0。   
  4.                         
  5. 渐变模式:   
  6. android:type="linear"       默认为线性渐变模式   
  7. android:type="radial"       径向渐变模式,需要指定半径  
  8. android:gradientRadius="50" 半径为50;
  9. android:type="sweep"     环形渐变模式

            stroke:描边

[html]  view plain copy print ?
  1. android:width="2dp" 描边的宽度   
  2. android:color       描边的颜色   
  3.                      
  4. 还可以把描边弄成虚线的形式,设置方式为:   
  5. android:dashWidth="5dp"表示'-'这样一个横线的宽度   
  6. android:dashGap="3dp"  表示'-'之间隔开的距离  

            corners:圆角  

[html]  view plain copy print ?
  1. android:radius  角的弧度,值越大角越圆   
  2.                    
  3. 还可以把四个角设定成不同的角度:   
  4.     <corners    
  5.        android:topRightRadius="20dp"     右上角   
  6.        android:bottomLeftRadius="20dp"   右下角   
  7.        android:topLeftRadius="1dp"       左上角   
  8.        android:bottomRightRadius="0dp"   左下角   
  9.      />  

        这里有个地方需要注意,bottomLeftRadius是右下角,而不是左下角,这个有点郁闷,不过不影响使用,记得别搞错了就行。


        padding:填充

android:bottom="1.0dip" 底部填充

android:left="1.0dip" 左边填充

android:right="1.0dip" 右边填充

android:top="0.0dip" 上面填充

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" />  
效果图:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值