Android solution to the outdated getColor() or getDrawable() method

Recently, when using context.getResources().getColor(R.color.color1) or getResources().getColor(R.color.color1), the system prompts that this method is outdated.

After Android 6.0, the system provides

ContextCompat.getColor(context, R.color.color1) 方法来代替原来的getColor()方法

The source code of ContextCompat.getColor() is as follows:

Similarly, when using the getResources().getDrawable() method, you can also use the ContextCompat.getDrawable() method instead.

The source code of the ContextCompat.getDrawable() method is as follows. The system automatically adapts it for us. It is recommended to use

If the article helps you, please give it a like and encourage it, thank you!

Guess you like

Origin blog.csdn.net/zhao8856234/article/details/107514453