本文主要是介绍android 背景选择器selector,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
点击查看转载原帖
一.创建xml文件,位置:drawable/xxx.xml,同目录下记得要放相关图片
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
- <!-- 没有焦点时的背景图片 -->
- <item android:drawable="@drawable/handle_normal" android:state_enabled="true" android:state_window_focused="false"/>
- <!-- 触摸模式按下时的背景图片 -->
- <item android:drawable="@drawable/handle_pressed" android:state_pressed="true"/>
- <item android:drawable="@drawable/handle_focused" android:state_enabled="true" android:state_focused="true"/>
- <item android:drawable="@drawable/handle_normal" android:state_enabled="true"/>
- <!-- 获得焦点时的图片背景 -->
- <item android:drawable="@drawable/handle_focused" android:state_focused="true"/>
- <!--
- android:state_selected是选中
- android:state_focused是获得焦点
- android:state_pressed是点击
- android:state_enabled是设置是否响应事件,指所有事件
- -->
- </selector>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><!-- 没有焦点时的背景图片 --><item android:drawable="@drawable/handle_normal" android:state_enabled="true" android:state_window_focused="false"/><!-- 触摸模式按下时的背景图片 --><item android:drawable="@drawable/handle_pressed" android:state_pressed="true"/><item android:drawable="@drawable/handle_focused" android:state_enabled="true" android:state_focused="true"/><item android:drawable="@drawable/handle_normal" android:state_enabled="true"/><!-- 获得焦点时的图片背景 --><item android:drawable="@drawable/handle_focused" android:state_focused="true"/><!--android:state_selected是选中android:state_focused是获得焦点android:state_pressed是点击android:state_enabled是设置是否响应事件,指所有事件--></selector>
二.使用xml文件:
1.方法一:在listview中配置android:listSelector="@drawable/xxx
或者在listview的item中添加属性android:background="@drawable/xxx"
2.方法二:Drawable drawable = getResources().getDrawable(R.drawable.xxx);
相关属性:
android:state_selected是选中
android:state_focused是获得焦点
android:state_pressed是点击
android:state_enabled是设置是否响应事件,指所有事件
根据这些状态同样可以设置button的selector效果。也可以设置selector改变button中的文字状态。
以下是配置button中的文字效果:
drawable/button_font.xml
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:state_selected="true" android:color="#FFF" />
- <item android:state_focused="true" android:color="#FFF" />
- <item android:state_pressed="true" android:color="#FFF" />
- <item android:color="#000" />
- </selector>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_selected="true" android:color="#FFF" /><item android:state_focused="true" android:color="#FFF" /><item android:state_pressed="true" android:color="#FFF" /><item android:color="#000" />
</selector>
Button还可以实现更复杂的效果,例如渐变
drawable/button_color.xml
- <span style="color: rgb(51, 0, 51);"><?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android"> /
- <item android:state_pressed="true">//定义当button 处于pressed 状态时的形态。
- <shape>
- <gradient android:startColor="#8600ff" />
- <stroke android:width="2dp" android:color="#000000" />
- <corners android:radius="5dp" />
- <padding android:left="10dp" android:top="10dp"
- android:bottom="10dp" android:right="10dp"/>
- </shape>
- </item>
- <item android:state_focused="true">//定义当button获得 focus时的形态
- <shape>
- <gradient android:startColor="#eac100"/>
- <stroke android:width="2dp" android:color="#333333" color="#ffffff"/>
- <corners android:radius="8dp" />
- <padding android:left="10dp" android:top="10dp"
- android:bottom="10dp" android:right="10dp"/>
- </shape>
- </item>
- </selector></span><span style="color: rgb(255, 0, 0);"> </span>
<span style="color: rgb(51, 0, 51);"><?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> /
<item android:state_pressed="true">//定义当button 处于pressed 状态时的形态。 <shape><gradient android:startColor="#8600ff" /> <stroke android:width="2dp" android:color="#000000" /> <corners android:radius="5dp" /> <padding android:left="10dp" android:top="10dp" android:bottom="10dp" android:right="10dp"/> </shape>
</item>
<item android:state_focused="true">//定义当button获得 focus时的形态 <shape> <gradient android:startColor="#eac100"/> <stroke android:width="2dp" android:color="#333333" color="#ffffff"/> <corners android:radius="8dp" /> <padding android:left="10dp" android:top="10dp" android:bottom="10dp" android:right="10dp"/> </shape> </item>
</selector></span><span style="color: rgb(255, 0, 0);"> </span>
这篇关于android 背景选择器selector的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!