getColorStateList has been deprecated

随声附和 提交于 2019-11-29 16:20:28

问题


I'm having a problem here. I've just updated from sdk 22 to 23, and the previous version of "getColorStateList()" has been deprecated.

My code was like this

seekBar.setProgressTintList(getResources().getColorStateList(R.color.bar_green));
valorslide.setTextColor(getResources().getColorStateList(R.color.text_green));

The older "getColorStateList" was

getColorStateList(int id)

And new one is

getColorStateList(int id, Resources.Theme theme)

How do I use the Theme variable? Thanks in advance


回答1:


The Theme object is the theme that is used to style the color state list. If you aren't using any special theming with individual resources, you can either pass null or the current theme as follows:

TextView valorslide; // initialize
SeekBar seekBar; // initialize
Context context = this;
Resources resources = context.getResources();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
    seekBar.setProgressTintList(resources.getColorStateList(R.color.bar_green, context.getTheme()));
    valorslide.setTextColor(resources.getColorStateList(R.color.text_green, context.getTheme()));
} else {
    seekBar.setProgressTintList(resources.getColorStateList(R.color.bar_green));
    valorslide.setTextColor(resources.getColorStateList(R.color.text_green));
}

If you don't don't care about the theme, you can just pass null:

getColorStateList(R.color.text_green, null)

See the documentation for more explanation. Note, you only need to use the new version on API 23 (Android Marshmallow) and above.




回答2:


While anthonycr's answer works, it is a lot more compact to just write

ContextCompat.getColorStateList(context, R.color.haml_indigo_blue);



回答3:


You need to use ContextCompat.getColor(), which is part of the Support V4 Library (so it will work for all the previous API).

ContextCompat.getColor(context, R.color.my_color)



回答4:


Exactly if you are using them , you will lose all styles. For an older version you should create ColorStateList dynamically, This is main chance to keep your styles.

this works for all versions

layout.setColorStateList(buildColorStateList(this,
   R.attr.colorPrimaryDark, R.attr.colorPrimary)
);


public ColorStateList buildColorStateList(Context context, @AttrRes int pressedColorAttr, @AttrRes int defaultColorAttr){
    int pressedColor = getColorByAttr(context, pressedColorAttr);
    int defaultColor = getColorByAttr(context, defaultColorAttr);

    return new ColorStateList(
            new int[][]{
                    new int[]{android.R.attr.state_pressed},
                    new int[]{} // this should be empty to make default color as we want
            }, new int[]{
            pressedColor,
            defaultColor
    }
    );
}

@ColorInt
public static int getColorByAttr(Context context, @AttrRes int attrColor){

    if (context == null || context.getTheme() == null)
        return -1;

    Resources.Theme theme = context.getTheme();
    TypedValue typedValue = new TypedValue();

    theme.resolveAttribute(attrColor, typedValue,true);

    return typedValue.data;
} 


来源:https://stackoverflow.com/questions/32359832/getcolorstatelist-has-been-deprecated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!