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圓角,代碼中動態修改邊線及填充色


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM