学习笔记_android之Selector与Shape的基本用法

1. Selector 介绍

drawable 的 item 中可以有以下属性:

android:drawable ="@drawable/drawable_resource"  放一个drawable资源

android:state_pressed =["true"| "false"] 是否按下,如一个按钮触摸或者点击。

android:state_focused =["true"| "false"] 是否取得焦点,比如用户选择了一个文本框。   

android:state_selected =["true"| "false"] 被选中,它与focus state并不完全一样,如一个list view 被选中的时候,它里面的各个子组件可能通过方向键,被选中了。  

android:state_active =["true"| "false"]    

android:state_checkable =["true"| "false"] 是否可选择,组件是否能被check。如:RadioButton是可以被check的。   

android:state_checked =["true"| "false"] 选择,被checked了,如:一个RadioButton可以被check了。  

android:state_enabled =["true"| "false"] 能够接受触摸或者点击事件   

android:state_window_focused =["true"| "false"] 应用程序是否在前台,当有通知栏被拉下来或者一个对话框弹出的时候应用程序就不在前台了

android:state_hovered 光标是否悬停,通常与focused state相同,它是4.0的新特性

android:state_activated 被激活

注意:如果有多个item,那么程序将自动从上到下进行匹配,最先匹配的将得到应用。(不是通过最佳匹配)如果一个item没有任何的状态说明,那么它将可以被任何一个状态匹配。

 

2.Shape 介绍

下面介绍Shape的用法:

<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 左下圆角半径

 

3.用selector添加shape

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_selected="true">

        <shape>

            <gradient android:angle="270" android:endColor="#99BD4C"

                android:startColor="#A5D245" />

            <size android:height="60dp" android:width="320dp" />

            <corners android:radius="8dp" />

        </shape>

    </item>

    <item android:state_pressed="true">

        <shape>

            <gradient android:angle="270" android:endColor="#99BD4C"

                android:startColor="#A5D245"/>

            <size android:height="60dp" android:width="320dp" />

            <corners android:radius="8dp" />

        </shape>

    </item>

    <item>

        <shape>

            <gradient android:angle="270" android:endColor="#A8C3B0"

                android:startColor="#C6CFCE"/>

            <size android:height="60dp" android:width="320dp" />

            <corners android:radius="8dp" />

        </shape>

    </item>

</selector>

 

4. 用法

第一种是在 listview 中配置:

android:listSelector ="@drawable/list_item_bg"

第二种是在listviewitem中添加属性:

androidbackground ="@drawable/list_item_bg"

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

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

listView.setSelector(drawable);

例:list_item_bg.xml

<?xml version="1.0"encoding="utf-8"?> 

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:state_pressed="true"> 

        <shape> 

            <!-- 渐变 --> 

            <gradient 

                android:startColor="#ff8c00"

                android:endColor="#FFFFFF"

                android:type="radial"

                android:gradientRadius="50"/> 

             <!-- 描边 --> 

             <stroke 

                 android:width="2dp"

                 android:color="#dcdcdc"

                 android:dashWidth="5dp"

                 android:dashGap="3dp"/> 

             <!-- 圆角 --> 

             <corners 

                 android:radius="2dp"/> 

             <padding 

                 android:left="10dp"

                 android:top="10dp"

                 android:right="10dp"

                 android:bottom="10dp"/> 

         </shape> 

     </item> 

      

     <item android:state_focused="true"> 

         <shape> 

             <gradient 

                android:startColor="#ffc2b7"

                android:endColor="#ffc2b7"

                android:angle="270"/> 

            <stroke 

                android:width="2dp"

                android:color="#dcdcdc"/> 

            <corners 

                android:radius="2dp"/> 

            <padding 

                android:left="10dp"

                android:top="10dp"

                android:right="10dp"

                android:bottom="10dp"/> 

        </shape> 

    </item> 

      

    <item>        

        <shape> 

            <solid android:color="#ff9d77"/> 

            <stroke 

                android:width="2dp"

                android:color="#fad3cf"/> 

            <corners  

                android:topRightRadius="5dp"

                android:bottomLeftRadius="5dp"

                android:topLeftRadius="0dp"

                android:bottomRightRadius="0dp"/>

            <padding 

                android:left="10dp"

                android:top="10dp"

                android:right="10dp"

                android:bottom="10dp"/> 

        </shape> 

    </item> 

</selector>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值