Android中selector背景选择器
2015-07-17 14:21
453 查看
在Android开发过程中,经常对某一View的背景在不同的状态下,设置不同的背景,增强用户体验。如果按钮,在按下时,背景变化,如果在代码中动态设置,相对比较麻烦。Android为我们提供了selector背景选择器可以非常方便的解决这一问题。
Selector的结构描述:
1.android:state_pressed="true/false"
true:表示按下状态下使用,false:表示非按下状态下使用。
2.android:state_focused="true/false"
ture:表示聚焦状态使用(例如使用滚动球/D-pad聚焦Button),false:表示非聚集状态下使用。
3.android:state_selected="true/false"
true:表示被选中状态下使用,false:表示非选中下使用
4.android:state_active="true/false"
true:表示可勾选状态时使用,false:表示不可勾选状态下使用
5. android:state_checkable="true/false"
true:表示勾选状态下使用,false:表示非勾选状态使用
6.android:state_checked="true/false"
true:表示勾选状态下使用,false:表示非勾选状态使用
7. android:state_enabled="true/false"
true:表示可用状态使用(能接收触摸/点击事件),false:表示不可用状态使用
8. android:state_window_focused="true/false"
true:表示应用程序窗口有焦点时使用(应用程序在前台),false:表示无焦点时使用
9.android:background
设置背景图片 模拟灯开启关闭
在drawable目录先新建bg_button.xml
[html]
view plaincopyprint?
<?xml version="1.0" encoding="UTF-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:drawable="@drawable/r7"></item> <item android:state_checked="false" android:drawable="@drawable/r7b"></item> </selector>
为了方便点击查看效果 使用CheckBox组件
[html]
view plaincopyprint?
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@null"
android:textSize="15sp"
android:textColor="#EE2C2C"
android:drawableTop="@drawable/bg_button"
android:text="灯"/>
效果:
在Android开发过程中,经常对某一View的背景在不同的状态下,设置不同的背景,增强用户体验。如果按钮,在按下时,背景变化,如果在代码中动态设置,相对比较麻烦。Android为我们提供了selector背景选择器可以非常方便的解决这一问题。
Selector的结构描述:
1.android:state_pressed="true/false"
true:表示按下状态下使用,false:表示非按下状态下使用。
2.android:state_focused="true/false"
ture:表示聚焦状态使用(例如使用滚动球/D-pad聚焦Button),false:表示非聚集状态下使用。
3.android:state_selected="true/false"
true:表示被选中状态下使用,false:表示非选中下使用
4.android:state_active="true/false"
true:表示可勾选状态时使用,false:表示不可勾选状态下使用
5. android:state_checkable="true/false"
true:表示勾选状态下使用,false:表示非勾选状态使用
6.android:state_checked="true/false"
true:表示勾选状态下使用,false:表示非勾选状态使用
7. android:state_enabled="true/false"
true:表示可用状态使用(能接收触摸/点击事件),false:表示不可用状态使用
8. android:state_window_focused="true/false"
true:表示应用程序窗口有焦点时使用(应用程序在前台),false:表示无焦点时使用
9.android:background
设置背景图片 模拟灯开启关闭
在drawable目录先新建bg_button.xml
[html]
view plaincopyprint?
<?xml version="1.0" encoding="UTF-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:drawable="@drawable/r7"></item> <item android:state_checked="false" android:drawable="@drawable/r7b"></item> </selector>
<?xml version="1.0" encoding="UTF-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:drawable="@drawable/r7"></item> <item android:state_checked="false" android:drawable="@drawable/r7b"></item> </selector>
为了方便点击查看效果 使用CheckBox组件
[html]
view plaincopyprint?
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@null"
android:textSize="15sp"
android:textColor="#EE2C2C"
android:drawableTop="@drawable/bg_button"
android:text="灯"/>
<CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:button="@null" android:textSize="15sp" android:textColor="#EE2C2C" android:drawableTop="@drawable/bg_button" android:text="灯"/>
效果:
相关文章推荐
- Android Studio中使用android:src="@drawable/ic_launcher"报错
- 手势
- Android 带清除 和 晃动效果的 自定义 EditText 解析
- Android开发中应该注意的事项
- 背景音乐(安卓Service)
- Android中的序列化浅析
- andriod studio 打包
- Android 中控件RecyclerView详解及实现瀑布流式布局
- Android自定义控件
- android CheckBox和右侧文字间距问题
- android textview 笔记
- Android中自定义弹窗提醒控件
- Android 手把手教您自定义ViewGroup(一)
- android:installLocation
- android Fragmentd的使用
- android intent隐式调用之一个应用程序启动另一个应用程序
- Android studio 混淆代码
- Android中Listview(五)--拖拽listview
- 【Android学习】实现卡片式ListView
- android:drawable的一些总结