• Android ColorStateList的基本使用


    ColorStateList

    参考:https://blog.csdn.net/zjh_1110120/article/details/89438309

    ColorStateList(颜色状态列表)是一个可以定义在 XML 布局文件中,并最终根据 ColorStateList 应用的 View 的状态显示不同颜色的对象。

    文件位置:res/color/filename.xml

    应用方式:

    1. In Java: R.color.filename
    2. In XML: @[package:]color/filename

    语法:

    <?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_checkable=["true" | "false"]
            android:state_checked=["true" | "false"]
            android:state_enabled=["true" | "false"]
            android:state_window_focused=["true" | "false"] />
    </selector>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    属性定义取值范围
    color不同状态的颜色值十六进制的颜色值。可以是如下格式: #RGB #ARGB #RRGGBB #AARRGGBB
    state_pressedView 按下的状态true,false。true,按下;false,默认状态,即没有按下之前的状态。
    state_selectedView 选中的状态true,false。true,选中;false,未选中。

    查看Color state list resource

    示例:

    //1. text_color_state_list.xml
    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:color="@color/green_700" android:state_pressed="true" />
        <item android:color="@color/grey_700" android:state_pressed="false" />
        <!--默认项-->
        <item android:color="@color/grey_700" />
    </selector>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    注意:

    1.ColorStateList 中定义的默认 Item 一定要放在最下面

    2.ColorStateList 是不能用于 View 的 Background

    3.StateListDrawable 是不能用于 TextView 系的 TextColor

    代码控制:

    private void initView(){
        
            mAlphaB = findViewById(R.id.alphabet_b);
            ColorStateList colorStateList = createColorStateList(getResources().getColor(R.color.green_700), getResources().getColor(R.color.grey_700));
            mAlphaB.setTextColor(colorStateList);
            
        }
    
        private ColorStateList createColorStateList(int pressed, int normal) {
            //状态
            int[][] states = new int[2][];
            //按下
            states[0] = new int[] {android.R.attr.state_pressed};
            //默认
            states[1] = new int[] {};
            
            //状态对应颜色值(按下,默认)
            int[] colors = new int[] { pressed, normal};
            ColorStateList colorList = new ColorStateList(states, colors);
            return colorList;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
  • 相关阅读:
    Linux学习记录——이십구 网络基础(2)
    代理现货白银有什么手续
    存储器IP核与DDS信号发生
    Hololens开发之实时音视频通讯--01部署开发环境
    厉害了,腾讯内部都用的Spring+MyBatis源码手册,实战理论两不误
    [附源码]计算机毕业设计体育器材及场地管理系统Springboot程序
    【云原生】Docker 命令大全之镜像仓库
    2023NOIP A层联测9 长春花
    MySQL系列——索引介绍
    计算机网络---传输层的概述
  • 原文地址:https://blog.csdn.net/weixin_44008788/article/details/126491437