android ColorStateList

在java里

    public ColorStateList getSecondPro() {
//        int[] colors = new int[] { pressed, focused, normal, focused, unable, normal };
        int[] colors = new int[] { android.R.color.background_dark, android.R.color.background_dark
                , android.R.color.background_dark, android.R.color.background_dark, android.R.color.background_dark
                , android.R.color.background_dark };
        int[][] states = new int[6][];
        states[0] = new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled };
        states[1] = new int[] { android.R.attr.state_enabled, android.R.attr.state_focused };
        states[2] = new int[] { android.R.attr.state_enabled };
        states[3] = new int[] { android.R.attr.state_focused };
        states[4] = new int[] { android.R.attr.state_window_focused };
        states[5] = new int[] {};
        ColorStateList colorStateList = new ColorStateList(states,colors);
        return colorStateList;
    }

在xml中

<?xml version="1.0" encoding="utf-8"?>  
<selector xmlns:android="http://schemas.android.com/apk/res/android">  
    <item android:state_pressed="true"  
          android:color="#ffff0000"/> <!-- pressed -->  
    <item android:state_focused="true"  
          android:color="#ff0000ff"/> <!-- focused -->  
    <item android:color="#ff000000"/> <!-- default -->  
</selector>  

全部属性

<?xml version="1.0" encoding="utf-8"?>  
   <selector xmlns:android="http://schemas.android.com/apk/res/android" >  
       <item  
           android:color="hex_color"  
           android:state_pressed=["true" | "false"]  
           android:state_focused=["true" | "false"]  
           android:state_selected=["true" | "false"]  
           android:state_active=["true" | "false"]  
           android:state_checkable=["true" | "false"]  
           android:state_checked=["true" | "false"]  
           android:state_enabled=["true" | "false"]  
           android:state_window_focused=["true" | "false"] />  
   </selector>  
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容