Java代码更改shape和selector文件的颜色值

Android里面经常会使用shape或者selector来定制一些View的背景。那么如果想要动态更改shape或者seletor文件中的颜色值,该怎么处理呢?

一、Java代码更改shape的颜色值

shape文件如下:

复制代码
<?xml version="1.0" encoding="utf-8"?>
<!-- popwindow样式文本框区域背景 -->
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle">
    
    <!-- 圆角 -->
    <corners 
        android:radius="5dp"/>
    
    <!-- 描边 -->
    <stroke 
        android:width="1dp" 
        android:color="#709e9e9e"/>
    
    <!-- 内边距 -->
    <!-- <padding 
        android:left="5dp"
        android:right="5dp"
        android:top="5dp"
        android:bottom="10dp"/> -->

    <!-- 填充 -->
    <solid android:color="#ffffffff" />
    
</shape>
复制代码

 Java代码如下:

GradientDrawable myGrad = (GradientDrawable)view.getBackground();
myGrad.setColor(ContextCompat.getColor(mContext,R.color.spinnerpop_notedit_bg_color));

myGrad.setStroke(2,ContextCompat.getColor(mContext,R.color.spinnerpop_notedit_bg_color));

二、Java代码更改selector的颜色值

selector文件如下:

复制代码
<?xml version="1.0" encoding="utf-8"?>
<!-- popwindow样式下拉菜单列表项背景 -->
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    
    <!-- 触发时、按压时:上浅颜色->下深颜色 -->
    <!-- android:angle="270"自上而下
    android:angle="90"自下而上 -->
    <item android:state_focused="true">
        <shape>
            <!-- 填充-->
            <solid android:color="#709e9e9e"/>
            <!-- 描边 -->
            <!-- <stroke
                android:width="1dp"
                android:color="#709e9e9e"/> -->
            <!-- 圆角 -->
            <corners
                android:radius="5dp" />
        </shape>
    </item>
    
    <item android:state_pressed="true">
        <shape>
            <!-- 填充-->
            <solid android:color="#709e9e9e"/>
            <!-- 描边 -->
            <!-- <stroke
                android:width="1dp"
                android:color="#709e9e9e"/> -->
            <!-- 圆角 -->
            <corners
                android:radius="5dp" />
        </shape>
    </item>
    
    <!-- 正常时:透明色 -->
    <item>
        <shape>
            <!-- 填充-->
            <solid android:color="@android:color/transparent"/>
            <!-- 圆角 -->
            <!--<corners
                android:radius="5dp" />-->
        </shape>
    </item>

</selector>
复制代码

java代码如下:

因为StatelistDrawable内获取状态以及drawable的方法都是被隐藏的,所以只有利用Java的反射机制来获取各个状态,以及各个状态对应的drawable。

复制代码
       StateListDrawable mySelectorGrad = (StateListDrawable)view.getBackground();

            try {
                Class slDraClass = StateListDrawable.class;
                Method getStateCountMethod = slDraClass.getDeclaredMethod("getStateCount", new Class[0]);
                Method getStateSetMethod = slDraClass.getDeclaredMethod("getStateSet", int.class);
                Method getDrawableMethod = slDraClass.getDeclaredMethod("getStateDrawable", int.class);
                int count = (Integer) getStateCountMethod.invoke(mySelectorGrad, new Object[]{});//对应item标签
                Log.d(TAG, "state count ="+count);
                for(int i=0;i < count;i++) {
                    int[] stateSet = (int[]) getStateSetMethod.invoke(mySelectorGrad, i);//对应item标签中的 android:state_xxxx
                    if (stateSet == null || stateSet.length == 0) {
                        Log.d(TAG, "state is null");
                        GradientDrawable drawable = (GradientDrawable) getDrawableMethod.invoke(mySelectorGrad, i);//这就是你要获得的Enabled为false时候的drawable
                        drawable.setColor(Color.parseColor(checkColor));
                    } else {
                        for (int j = 0; j < stateSet.length; j++) {
                            Log.d(TAG, "state =" + stateSet[j]);
                            Drawable drawable = (Drawable) getDrawableMethod.invoke(mySelectorGrad, i);//这就是你要获得的Enabled为false时候的drawable

                        }
                    }
                }
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
复制代码

android:state_focused  对应的Java中的整型值 : android.R.attr.state_focused,如果为false,则对应 - android.R.attr.state_focused【“-”负号表示对应的属性值为false

android:state_pressed   对应的Java中的整型值 : android.R.attr.state_pressed,如果为false,则对应 - android.R.attr.state_pressed【“-”负号表示对应的属性值为false

参考资料

在使用shape的同时,用代码修改shape的颜色属性

获取xml里设置的statelistdrawable内的各个状态对应的drawable

android背景选择器selector用法汇总

关于自定义drawable圆角,代码中动态修改边线及填充色

posted @   HaiyuKing  阅读(3465)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
阅读排行:
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 推荐几款开源且免费的 .NET MAUI 组件库
· 实操Deepseek接入个人知识库
· 易语言 —— 开山篇
· Trae初体验
点击右上角即可分享
微信分享提示