Android 使用 selector 来改变TextView在不同状态下的textColor和background
2015-02-27 13:44
369 查看
最近在项目中需要实现一个功能:可以点击TextView,当TextView被点击的时候,背景变成了红色,字体颜色变成了白色,当不点击的时候,字体是灰色的,背景是白色的。为了能够实现这个功能,我想到了selector,于是,在drawable文件夹下面定义两个文件,分别用于TextView的textColor 和 background:
设置background :
设置textColor:
selector属性介绍:
android:state_selected选中
android:state_focused获得焦点
android:state_pressed点击
android:state_enabled设置是否响应事件,指所有事件
设置background :
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/course_pressed" android:state_pressed="true"/> <item android:drawable="@drawable/no_select"/> </selector>
设置textColor:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:color="@color/message_notice_selected"></item> <item android:color="@color/tab_text_unselected"></item> </selector>
selector属性介绍:
android:state_selected选中
android:state_focused获得焦点
android:state_pressed点击
android:state_enabled设置是否响应事件,指所有事件
相关文章推荐
- 使用Selector改变TextView的字体颜色textColor的方法
- android selector(如对TextView点击样式改变) - perfect亮
- Android中的如何在代码中使用64位颜色值改变TextView的颜色的方法
- android实战——监听TextView文本Button改变状态
- TextView按下background和textColor一起改变
- Android中View(视图)绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解
- Android 使用selector改变按钮状态实例详解
- Android中使用level-list改变View显示状态
- 【Android】在控件、视图绘制或改变之后如何获取其部分状态和属性(比如高、宽、TextView绘制后的文字行数等)
- 关于网络状态改变监听广播的使用(android不同系统版本会有不同)
- Android中View(视图)绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解
- Android中View(视图)绘制不同状态背景图片原理深入分析以及StateListDrawable使用详解
- android中的spannable的使用(TextView分段显示不同颜色,字体,时间 )
- Android中EditView TextView (padding失效)使用setBackgroundDrawable或setBackgroundResource则xml中设置的 Padding失效
- Android布局TextView文字设置不同样式Spannable的使用
- android TextView selector点击样式改变
- Android如何使用java改变TextView的宽高,设置全屏
- Android开发使用XML文件定义ImageView的在selected为true和false状态下显示不同的图像
- 点击textview 使用selector改变对应viewGroup(LinearLayout,RelativeLayout)的背景
- 点击LinearLayout使用selector改变TextView字体颜色