How to get a value of color attribute programmatically

前端 未结 3 2316
自闭症患者
自闭症患者 2021-02-20 05:01

When I use resolveAttribute() to find out a color value of ?attr/colorControlNormal, I got 236:

TypedValue typedValue = ne         


        
3条回答
  •  庸人自扰
    2021-02-20 05:31

    I believe instead of this:

    
        TypedValue typedValue = new TypedValue();
        getTheme().resolveAttribute(R.attr.colorControlNormal, typedValue, true);
        int color = typedValue.data;
    
    

    You should do this:

    
        TypedValue typedValue = new TypedValue();
        getTheme().resolveAttribute(R.attr.colorControlNormal, typedValue, true);
        int color = ContextCompat.getColor(this, typedValue.resourceId)
    
    

提交回复
热议问题