浅谈android的selector,背景选择器

最近做listview和button都要改变android原来控件的背景,在网上查找了一些资料不是很全,所以现在总结一下android的selector的用法。
首先android的selector是在drawable/xxx.xml中配置的。
先看一下listview中的状态:
把下面的XML文件保存成你自己命名的.xml文件(比如list_item_bg.xml),在系统使用时根据ListView中的列表项的状态来使用相应的背景图片。
drawable/list_item_bg.xml
<?xml version="1.0" encoding="utf-8" ?>   
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<!-- 默认时的背景图片 -->  
  <item android:drawable="@drawable/pic1" />    
<!-- 没有焦点时的背景图片 -->  
  <item android:state_window_focused="false"   
        android:drawable="@drawable/pic1" />   
<!-- 非触摸模式下获得焦点并单击时的背景图片 -->  
  <item android:state_focused="true" android:state_pressed="true"   
        android:drawable= "@drawable/pic2" />  
<!-- 触摸模式下单击时的背景图片 -->  
  <item android:state_focused="false" android:state_pressed="true"   
        android:drawable="@drawable/pic3" />   
<!--选中时的图片背景  -->  
  <item android:state_selected="true"   
        android:drawable="@drawable/pic4" />   
<!--获得焦点时的图片背景  -->  
  <item android:state_focused="true"   
        android:drawable="@drawable/pic5" />   
</selector>
使用些xml文件:第一种是在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);同样的效果。
但是这样会出现列表有时候为黑的情况,需要加上:android:cacheColorHint="@android:color/transparent"
使其透明。
其次再来看看Button的一些背景效果:
android:state_selected是选中
android:state_focused是获得焦点
android:state_pressed是点击
android:state_enabled是设置是否响应事件,指所有事件
根据这些状态同样可以设置button的selector效果。也可以设置selector改变button中的文字状态。

http://blog.csdn.net/bossgirls/article/details/7578049

Selector背景选择器可以帮助我们切换自定义的背景风格,用于buttonListView布局点击时候的背景切换

Selector包含一个或多个item元素

常用item的属性:

<item>

      android:state_focused   ==》true表示获得焦点时显示(例如用滚动球聚焦button),false表示没获得焦点时显示默认  

     android:state_window_focused==》true表示应用程序窗口有焦点时使用;false表示无焦点时使用(例如Notification栏拉下或对话框显示)

     "true" if this item should be used when the application window has focus (the application is in the foreground), "false" if this item should be used when the application window does not have focus (for example, if the notification shade is pulled down or a dialog appears).

     android:state_enabled==》true表示object(组件)能使用时(显示该图片)(能够接受触摸事件和单击事件),false表示该组件不能使用时(显示该图片)

     android:state_pressed ==》true表示当object被按下(例如一个按钮被触摸若点击)时使用(显示按下图片)false表示没被按下时显示默认(图片)

      android:state_checkable ==》true表示当组件被选中时使用,false:组件没被选中时使用

       "true" if this item should be used when the object is checkable; "false" if this item should be used when the object is not checkable. (Only useful if the object can transition between a checkable and non-checkable widget.)

     android:state_checked ==》true表示被选中时使用,false表示没被选中时使用

     "true" if this item should be used when the object is checked; "false" if it should be used when the object is un-checked true表示当checkbox选中时显示该图片,false当checkbox

     android:state_selected ==》true表示当组件被选择时使用(例如当一个tab组件被选中),false当组件未被选择时使用

     "true" if this item should be used when the object is selected (such as when a tab is opened); "false" if this item should be used when the object is not selected. 

</item>


下面先从自定义button背景风格展开(ListView和布局的以后会补上)

一、button的背景效果

1.点击button背景图的切换

如果要设置点击button,button背景切换的话,最简单的方法就是只需要两个item标签就可以搞定:

【注意点微笑^_<耐心读下去啦】注意item的顺序,第一个匹配当前状态的item会被使用,因为有item已经实现了,所以就会返回不去执行下面的item,就像程序里的if语句一样。所以返回条件限制越细致的要放在前面,而对于默认条件下的item必须放在最后面。拿上面的那个例子来说,如果你把最后一句默认显示图片btn_n的item语句放在最上面,那么当你点击按钮的时候是不会有按下去的效果的,因为点击按钮后item实现了第一句默认显示图片,就返回了,state_pressed不会被触发,就没有按下去的效果。所以item的顺序要关注哦~


2.用shape自定义button的风格

这个给个链接吧,我觉得以后用到的不多,因为这样的按钮其实蛮费时的还不一定美观,还是选择ps吧

点击链接:Android Selector与shape基本用法

http://mobile.51cto.com/android-266469.htm

最近做listview和button都要改变Android原来控件的背景,在网上查找了一些资料不是很全,所以现在总结一下android的selector的用法。

首先android的selector是在drawable/xxx.xml中配置的。

先看一下listview中的状态:

把下面的XML文件保存成你自己命名的.xml文件(比如list_item_bg.xml),在系统使用时根据ListView中的列表项的状态来使用相应的背景图片。drawable/list_item_bg.xml

Java代码:

 
 
  1. <?xml version="1.0" encoding="utf-8" ?>   
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">   
  3. <!-- 默认时的背景图片 -->   
  4. <item android:drawable="@drawable/pic1" />   
  5. <!-- 没有焦点时的背景图片 -->   
  6. <item android:state_window_focused="false"   
  7. android:drawable="@drawable/pic1" />   
  8. <!-- 非触摸模式下获得焦点并单击时的背景图片 -->   
  9. <item android:state_focused="true" android:state_pressed="true"   
  10. android:drawable"@drawable/pic2" />   
  11. <!-- 触摸模式下单击时的背景图片 -->   
  12. <item android:state_focused="false" android:state_pressed="true"   
  13. android:drawable="@drawable/pic3" />   
  14. <!--选中时的图片背景 -->   
  15. <item android:state_selected="true"   
  16. android:drawable="@drawable/pic4" />   
  17. <!--获得焦点时的图片背景 -->   
  18. <item android:state_focused="true"   
  19. android:drawable="@drawable/pic5" />   
  20. </selector>  

使用些xml文件:第一种是在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);同样的效果。

但是这样会出现列表有时候为黑的情况,需要加上:android:cacheColorHint="@android:color/transparent"

使其透明。

其次再来看看Button的一些背景效果:

android:state_selected是选中

android:state_focused是获得焦点

android:state_pressed是点击

android:state_enabled是设置是否响应事件,指所有事件

根据这些状态同样可以设置button的selector效果。也可以设置selector改变button中的文字状态。以下就是配置button中的文字效果:drawable/button_font.xml

Java代码:

 
 
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">   
  3. <item android:state_selected="true" android:color="#FFF" />   
  4. <item android:state_focused="true" android:color="#FFF" />   
  5. <item android:state_pressed="true" android:color="#FFF" />   
  6. <item android:color="#000" />   
  7. </selector>  

Button还可以实现更复杂的效果,例如渐变啊等等。

Java代码:

 
 
  1. drawable/button_color.xml   
  2. <?xml version="1.0" encoding="utf-8"?>   
  3. <selector xmlns:android="http://schemas.android.com/apk/res/android"> /   
  4. <item android:state_pressed="true">//定义当button 处于pressed 状态时的形态。   
  5. <shape>   
  6. <gradient android:startColor="#8600ff" />   
  7. <stroke android:width="2dp" android:color="#000000" />   
  8. <corners android:radius="5dp" />   
  9. <padding android:left="10dp" android:top="10dp"   
  10. android:bottom="10dp" android:right="10dp"/>   
  11. </shape>   
  12. </item>   
  13. <item android:state_focused="true">//定义当button获得 focus时的形态   
  14. <shape>   
  15. <gradient android:startColor="#eac100"/>   
  16. <stroke android:width="2dp" android:color="#333333" color="#ffffff"/>   
  17. <corners android:radius="8dp" />   
  18. <padding android:left="10dp" android:top="10dp"   
  19. android:bottom="10dp" android:right="10dp"/>   
  20. </shape>   
  21. </item>   
  22. </selector>  

最后,需要在包含 button的xml文件里添加两项。假如是 main.xml 文件,我们需要在<Button />里加两项。

 
 
  1. android:focusable="true" 
  2. android:backgroud="@drawable/button_color" 

这样当你使用Button的时候就可以甩掉系统自带的那黄颜色的背景了,实现个性化的背景,配合应用的整体布局非常之有用啊。




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值