Android_布局Selector与Shape的基本用法

情景再现:UI按照720P切图,缩放成.9图片缩放后边角会出现虚化~特此要用代码解决~

1. Selector


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"]  

2. Shape


                solid:实心,就是填充     

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

                gradient:渐变

[html]  view plain copy print ?
  1. android:startColor  起始颜色   
  2. android:endColor    结束颜色   
  3. android:angle       渐变角度,必须为45的整数倍。   
  4.                         
  5. 渐变模式:   
  6. android:type="linear"       默认为线性渐变模式   
  7. android:type="radial"       径向渐变,需要指定半径   
  8. android:gradientRadius="50" 半径为50  

                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:间隔

 

3. 用法:


第一种是在 listview 中配置:

android:listSelector = "@drawable/list_item_bg"

第二种是在listview的item中添加属性:


android:background = "@drawable/list_item_bg"

第三种是在Java代码中使用:


Drawable drawable = getResources().getDrawable(R.drawable.list_item_bg); 
listView.setSelector(drawable);

 

4. 例:list_item_bg.xml

[html]  view plain copy print ?
  1. <?xml version="1.0"encoding="utf-8"?>   
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">   
  3.     <item android:state_pressed="true">   
  4.         <shape>   
  5.             <!-- 渐变 -->   
  6.             <gradient   
  7.                 android:startColor="#ff8c00"  
  8.                 android:endColor="#FFFFFF"  
  9.                 android:type="radial"  
  10.                 android:gradientRadius="50"/>   
  11.              <!-- 描边 -->   
  12.              <stroke   
  13.                  android:width="2dp"  
  14.                  android:color="#dcdcdc"  
  15.                  android:dashWidth="5dp"  
  16.                  android:dashGap="3dp"/>   
  17.              <!-- 圆角 -->   
  18.              <corners   
  19.                  android:radius="2dp"/>   
  20.              <padding   
  21.                  android:left="10dp"  
  22.                  android:top="10dp"  
  23.                  android:right="10dp"  
  24.                  android:bottom="10dp"/>   
  25.          </shape>   
  26.      </item>   
  27.         
  28.      <item android:state_focused="true">   
  29.          <shape>   
  30.              <gradient   
  31.                 android:startColor="#ffc2b7"  
  32.                 android:endColor="#ffc2b7"  
  33.                 android:angle="270"/>   
  34.             <stroke   
  35.                 android:width="2dp"  
  36.                 android:color="#dcdcdc"/>   
  37.             <corners   
  38.                 android:radius="2dp"/>   
  39.             <padding   
  40.                 android:left="10dp"  
  41.                 android:top="10dp"  
  42.                 android:right="10dp"  
  43.                 android:bottom="10dp"/>   
  44.         </shape>   
  45.     </item>   
  46.         
  47.     <item>          
  48.         <shape>   
  49.             <solid android:color="#ff9d77"/>   
  50.             <stroke   
  51.                 android:width="2dp"  
  52.                 android:color="#fad3cf"/>   
  53.             <corners    
  54.                 android:topRightRadius="5dp"  
  55.                 android:bottomLeftRadius="5dp"  
  56.                 android:topLeftRadius="0dp"  
  57.                 android:bottomRightRadius="0dp"/>  
  58.     
  59.             <padding   
  60.                 android:left="10dp"  
  61.                 android:top="10dp"  
  62.                 android:right="10dp"  
  63.                 android:bottom="10dp"/>   
  64.         </shape>   
  65.     </item>   
  66. </selector>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值