getColorStateList 已被弃用
getColorStateList has been deprecated
我这里有问题。我刚刚从 sdk 22 更新到 23,之前的版本 "getColorStateList()" 已经被弃用了。
我的代码是这样的
seekBar.setProgressTintList(getResources().getColorStateList(R.color.bar_green));
valorslide.setTextColor(getResources().getColorStateList(R.color.text_green));
年长的 "getColorStateList" 是
getColorStateList(int id)
新的是
getColorStateList(int id, Resources.Theme theme)
如何使用主题变量?提前致谢
Theme 对象是用于设置颜色状态列表样式的主题。如果您没有对个人资源使用任何特殊主题,您可以传递 null
或当前主题,如下所示:
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));
}
如果不关心主题,直接传null即可:
getColorStateList(R.color.text_green, null)
See the documentation for more explanation. 注意,您只需要在 API 23 (Android Marshmallow) 及更高版本上使用新版本。
虽然 anthonycr 的答案有效,但只写
会更紧凑
ContextCompat.getColorStateList(context, R.color.haml_indigo_blue);
您需要使用 ContextCompat.getColor(),它是 Support V4 库的一部分(因此它适用于所有以前的 API)。
ContextCompat.getColor(context, R.color.my_color)
确切地说,如果您正在使用它们,您将失去所有样式。对于旧版本,您应该动态创建 ColorStateList
,这是保留样式的主要机会。
这适用于所有版本
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;
}
还有一个更新的方法:
AppCompatResources.getColorStateList(context, R.color.bar_green)
这会保留对已膨胀的 ColorStateList 缓存的弱引用,如果加载失败,它会退回到 ContextCompat.getColorStateList
。
我这里有问题。我刚刚从 sdk 22 更新到 23,之前的版本 "getColorStateList()" 已经被弃用了。
我的代码是这样的
seekBar.setProgressTintList(getResources().getColorStateList(R.color.bar_green));
valorslide.setTextColor(getResources().getColorStateList(R.color.text_green));
年长的 "getColorStateList" 是
getColorStateList(int id)
新的是
getColorStateList(int id, Resources.Theme theme)
如何使用主题变量?提前致谢
Theme 对象是用于设置颜色状态列表样式的主题。如果您没有对个人资源使用任何特殊主题,您可以传递 null
或当前主题,如下所示:
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));
}
如果不关心主题,直接传null即可:
getColorStateList(R.color.text_green, null)
See the documentation for more explanation. 注意,您只需要在 API 23 (Android Marshmallow) 及更高版本上使用新版本。
虽然 anthonycr 的答案有效,但只写
会更紧凑ContextCompat.getColorStateList(context, R.color.haml_indigo_blue);
您需要使用 ContextCompat.getColor(),它是 Support V4 库的一部分(因此它适用于所有以前的 API)。
ContextCompat.getColor(context, R.color.my_color)
确切地说,如果您正在使用它们,您将失去所有样式。对于旧版本,您应该动态创建 ColorStateList
,这是保留样式的主要机会。
这适用于所有版本
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;
}
还有一个更新的方法:
AppCompatResources.getColorStateList(context, R.color.bar_green)
这会保留对已膨胀的 ColorStateList 缓存的弱引用,如果加载失败,它会退回到 ContextCompat.getColorStateList
。