码迷,mamicode.com
首页 > 移动开发 > 详细

Android:自定义控件样式(Selector)

时间:2014-06-04 15:26:41      阅读:335      评论:0      收藏:0      [点我收藏+]

标签:des   android   c   style   class   blog   

前言

  在开发一个应用程序过程中不可避免的要去修改组件的样式,比如按钮、输入框等。现在就看下如何通过Seletor实现样式的自定义。先看下简单的效果对比

bubuko.com,布布扣

概要实现

  首先写这个Selector XML文件,叫做button_selector,放到了drawable文件夹下,大概内容如下所示

  

bubuko.com,布布扣
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <selector xmlns:android="http://schemas.android.com/apk/res/android">
 3 
 4     <!-- 按下状态 -->
 5     <item android:state_pressed="true">
 6         <shape >
 7               <!-- 边框颜色 -->
 8             <stroke android:width="1dip" android:color="#728ea3" />
 9             <!-- 背景颜色 -->
10             <solid android:color="#FFFFcc" />  
11         </shape>
12         
13     </item>
14     <!-- 默认状态 -->
15     <item>
16         <shape>
17             <!-- 边框颜色 -->
18             <stroke android:width="1dip" android:color="#728ea3" />
19             <!-- 背景颜色 -->
20             <solid android:color="#FFFFFF" />   
21         </shape>      
22     </item>
23 
24 </selector>
bubuko.com,布布扣

  然后为该按钮设置background属性:@drawable/button_selector,如下所示

  

bubuko.com,布布扣
<Button
        android:id="@+id/btnSelector"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/button_selector"
        android:text="Selector演示"
 />
bubuko.com,布布扣

  这样自定义样式就成功的应用到了这个按钮上了。

Selector

  先来看下官方描述:

  You can describe the state list in an XML file. Each graphic is represented by an <item> element inside a single <selector> element. Each <item> uses various attributes to describe the state in which it should be used as the graphic for the drawable.

  意思是说:你可以用一个XML文件来描述状态列表。在唯一的selector节点下,用item来描述每一种状态。每一个item通过不同的属性来标识用于哪种属性。

  下面就看下item的具体属性

  android:drawable:引用一个drawable资源

  android:state_pressed:Boolean值,如果设置为true则代表用于对象在被按下的时候

  android:state_focused:Boolean值,如果设置为true则代表用于对象在获得焦点的时候

  android:state_hovered:Boolean值,如果设置为true则代表用于对象在hover状态的时候

  android:state_selected:Boolean值,如果设置为true则代表用于对象在选中的时候

  android:state_checkable:Boolean值,如果设置为true则代表用于对象允许选中的时候

  android:state_checked:Boolean值,如果设置为true则代表用于对象被选中的时候

  android:state_enabled:Boolean值,如果设置为true则代表用于对象可用的时候(响应触摸或点击事件)

  android:state_activated:Boolean值,如果设置为true则代表用于对象被激活的时候

  android:state_window_focused:Boolean值,如果设置为true则代表用于窗体获得焦点的时候

  通过以上属性,就可以灵活的定制出期望的结果了,这次例子只是展示了android:state_pressed这一种状态的效果。Selector代码为上文提到的button_selector。效果如下所示

  bubuko.com,布布扣

简单分析

  再来看下press的那个item内容。

  

bubuko.com,布布扣
<!-- 按下状态 -->
    <item android:state_pressed="true">
        <shape >
              <!-- 边框颜色 -->
            <stroke android:width="1dip" android:color="#728ea3" />
            <!-- 背景颜色 -->
            <solid android:color="#FFFFcc" />  
        </shape>
        
    </item>
bubuko.com,布布扣

  上图所示的按下效果是通过shape标签来完成的。这个标签用来指定背景的样式,由于这次重点介绍Selector的用法,Shape的用法就不过多解释了,只是把代码中出现的标签做下简单说明。

  stroke:用来设定背景边框的样式,可以去定义它的宽度(width),颜色(color),是否为虚线展示等等

  color:用来设定背景颜色

 

后记

  这篇文字只是简单的介绍了下Selector的大体用法,具体的灵活使用可以构造出很强大的显示效果。

  原文地址:http://www.cnblogs.com/luoaz/p/3764784.html

  完整Demo:https://github.com/xiaoai-opensource/Selector

Android:自定义控件样式(Selector),布布扣,bubuko.com

Android:自定义控件样式(Selector)

标签:des   android   c   style   class   blog   

原文地址:http://www.cnblogs.com/luoaz/p/3764784.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!