android中color-selector的使用

selector选择器,常用来做组件的背景,可以用代码来控制组件在不同状态的背景颜色或图片的切换。常用的是color-selector和drawable-selector,下面主要来介绍color-selector的使用。

Color-Selector

color-selector用来实现颜色状态列表,使用方法和color一样,颜色会随着组件的状态而改变。color-selector文件存储在如下目录:

//文件路径
/res/color/filename.xml

语法



    //是否窗口聚焦

示例

创建res/color/right_menu_text_color.xml文件,内容如下。实现将文字在disable时置灰且不可点击,而enable时文字点亮且可点击的功能



    
    
    

调用

//java调用注意一定要使用**getResources().getColorStateList(textColor)**方法,才能生效
textView.setTextColor(getResources().getColorStateList(R.color.right_menu_text_color));

//xml调用直接使用
android:textColor = "@color/filename"

你可能感兴趣的:(android中color-selector的使用)