美文网首页
2018-02-02 安卓备忘:shape与selector复制

2018-02-02 安卓备忘:shape与selector复制

作者: 心灵屋宿客 | 来源:发表于2018-02-02 14:18 被阅读0次

    这里总结一下shape与selector相关的属性,留作以后粘贴复制……。好吧,我承认我很懒~~

    所谓状态选择器,就是控件(view或者viewgroup)根据不同的选定状态来定义不同的现实效果,我们可以在指定的状态下,切换控件的背景属性(background),从而达到效果绚丽的目的。

    常用属性:

    属性 状态
    android:drawable 放一个drawable资源
    android:state_pressed 当前view是否被按下,如一个按钮触摸或者点击。
    android:state_focused 官方解释为当前view获得焦点的时候,比如用户选择了一个文本框。
    android:state_hovered 光标是否悬停,通常与focused state相同,它是4.0的新特性
    android:state_selected 被选中,它与focus state并不完全一样,如一个list view 被选中的时候,它里面的各个子组件可能通过方向键,被选中了。
    android:state_checkable 当前view是否可以被check。如:RadioButton是可以被check的。
    android:state_checked 被checked了,如:一个RadioButton可以被check了。
    android:state_enabled 当前view可以被点击或者触发触摸事件的时候,能够接受触摸或者点击事件
    android:state_activated 被激活,官方解释是set when a view or its parent has been "activated" meaning the user has currently marked it as being of interest.
    android:state_window_focused 当前view所在的窗体获得焦点的时候,应用程序是否在前台,当有通知栏被拉下来或者一个对话框弹出的时候应用程序就不在前台了

    有的需要加android:focusable和android:clickable为true才能获取焦点

    button_select.xml

    <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
        <item android:state_pressed="true" android:drawable="@drawable/play_button"></item>
        <item android:state_focused="true" android:drawable="@drawable/play_button"></item>
        <item android:drawable="@drawable/button"></item>
    </selector>  
    

    常用checkbox设置:

    <?xml version="1.0" encoding="UTF-8"?>
    <selector  xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_window_focused="false"
         android:state_enabled="true" 
         android:state_checked="true" 
         android:drawable="@drawable/btn_check_on" />
        <item android:state_window_focused="false"
         android:state_enabled="true" 
         android:state_checked="false" 
         android:drawable="@drawable/btn_check_off" />
        <item android:state_enabled="true" 
        android:state_checked="true" 
        android:state_pressed="true" 
        android:drawable="@drawable/btn_check_on_pressed" />
        <item android:state_enabled="true" android:state_checked="false" android:state_pressed="true" android:drawable="@drawable/btn_check_off_pressed" />
        <item android:state_focused="true" 
        android:state_enabled="true" 
        android:state_checked="true"
        android:drawable="@drawable/btn_check_on_selected" />
        <item android:state_focused="true" android:state_enabled="true" android:state_checked="false" android:drawable="@drawable/btn_check_off_selected" />
        <item android:state_enabled="true"
         android:state_checked="false" 
         android:drawable="@drawable/btn_check_off" />
        <item android:state_enabled="true" 
         android:state_checked="true"
         android:drawable="@drawable/btn_check_on" />
    </selector>
    

    常用ImageButton设置:

    <?xml version="1.0" encoding="UTF-8"?>
    <selector  xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true" android:drawable="@drawable/button2_down" />
        <item android:state_focused="true" android:state_enabled="true" android:drawable="@drawable/button2_over" />
        <item android:state_enabled="true" android:drawable="@drawable/button2" />
    </selector>
    

    常用Button设置:

    <?xml version="1.0" encoding="UTF-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_enabled="false" android:drawable="@drawable/login_input" />
        <item android:state_pressed="true" android:drawable="@drawable/login_input" />
        <item android:state_focused="true" android:drawable="@drawable/input_over" />
    </selector>
    

    设置TextView

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android" >
        <item android:color="@color/gray" android:state_pressed="false" android:state_enabled="true"/>
        <item android:color="@color/white" android:state_pressed="true" android:state_enabled="true"/>
        <item android:color="@color/gray" android:state_enabled="false"/>
    </selector>
    

    drawble资源,才是状态选择器的重点。首先,drawble资源比较复杂的时候,我们一般可以用.9patch图片来替代,完美适配。但比较简单的时候,我们可以自己去写背景的drawble资源的。那么,如何自定义一个drawble资源呢?下面来讲讲android下shape的使用。
    先来看例子:

    <?xml version="1.0" encoding="utf-8"?>  
    <shape xmlns:android="http://schemas.android.com/apk/res/android"  
        android:shape="" >  
      
        <!-- 圆角 -->  
        <corners  
            android:radius="10dp"  
            android:topLeftRadius="" />  
        <!-- 描边 -->  
        <stroke  
            android:dashGap=""  
            android:dashWidth=""  
            android:width="2dp"  
            android:color="@color/darkgray" />  
        <!-- 实心 -->  
        <solid android:color="#c4c4c4" />  
      
        <!-- 大小 -->  
        <size  
            android:height=""  
            android:width="" />  
        <!-- 颜色渐变 -->  
      
        <gradient  
            android:angle=""  
            android:centerColor=""  
            android:endColor=""  
            android:gradientRadius=""  
            android:startColor=""  
            android:type="" />  
    </shape>  
    

    首先是最开始的shape标签。在shape里面有个shape属性,这个属性可以设定,也可以不设定,不设定的时候默认是矩形。设定有四个值可以设定:
    1、rectangle 矩形
    2、oval 椭圆形 当宽高设定为相同的时候,就是圆
    3、line 线性形状
    4、ring 环形 可用作数据刷新时转圈的提示
    当设定为ring环形的时候,还需要设定一下几个属性

     android:innerRadiusRatio="3"  //浮点型数据,以环的宽度比率来表示内环的半径
     android:thicknessRatio="8"    //浮点型数据,以环的宽度比率来表示环的厚度
     android:useLevel="false"  //如果当做是LevlListDrawable使用时为true,其他为false
    

    接下来定义在shape标签里面的节点

    <!-- 圆角 -->  
       <corners  
           android:radius="10dp"  
           android:topLeftRadius="" />  
    

    这个表示圆角,可以一次性设定四个边角的大小。也分个设定四个角度的大小,这里只写了全部的和左上角的。

    <!-- 描边 -->  
    <stroke  
       android:dashGap=""  
       android:dashWidth=""  
       android:width="2dp"  
       android:color="@color/darkgray" />  
    

    这个表示描边,在边界画线。width表示线的厚度,color表示颜色,dashWidth和dashGap是用来画虚线的时候用的,dashWidth表示虚线的宽度,dashGap表示虚线的间隔。

    <!-- 实心 -->  
      <solid android:color="#c4c4c4" />  
    

    这个没什么好说的。

    <!-- 大小 -->  
     <size  
         android:height=""  
         android:width="" />  
    
    <!-- 颜色渐变 -->    
     <gradient  
         android:angle=""  
         android:centerColor=""  
         android:endColor=""  
         android:gradientRadius=""  
         android:startColor=""  
         android:centerX=""  
         android:centerY=""  
         android:gradientRadius=""  
         android:type="" />  
    

    颜色渐变需要好好讲解一下。
    angle表示颜色渐变的起始位置,0表示从左向右然后逆时针方向,90表示从上到下,以此类推,angle必须为45点整数倍
    startColor endColor centerColor,颜色 渐变 过程的颜色值。
    type,颜色渐变类型,有三个值
    1、linear,线性渐变,这个是默认值
    2、radial,放射性渐变,这个要配合android:gradientRadius属性使用,android:gradientRadius表示放射渐变的半径大小。
    3、sweep,扫描石渐变,就像雷达扫描的那个效果。
    centerX,centerY,表示渐变中心的X和Y点的坐标的相对位置。

    先放到这里了,有时间了再整理~

    相关文章

      网友评论

          本文标题:2018-02-02 安卓备忘:shape与selector复制

          本文链接:https://www.haomeiwen.com/subject/bagszxtx.html