下面列出了android.content.Context#getColorStateList ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private CharSequence formatUrl(Context context, CharSequence url) {
if (mUrlColor == null) {
// Lazily get the URL color from the current theme.
TypedValue colorValue = new TypedValue();
context.getTheme().resolveAttribute(R.attr.textColorSearchUrl, colorValue, true);
mUrlColor = context.getColorStateList(colorValue.resourceId);
}
SpannableString text = new SpannableString(url);
text.setSpan(new TextAppearanceSpan(null, 0, 0, mUrlColor, null),
0, url.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return text;
}
public static ColorStateList getActionTextColorStateList(Context context, @ColorRes int colorId) {
final TypedValue value = new TypedValue();
context.getResources().getValue(colorId, value, true);
if (value.type >= TypedValue.TYPE_FIRST_COLOR_INT && value.type <= TypedValue.TYPE_LAST_COLOR_INT) {
return getActionTextStateList(context, value.data);
} else {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1) {
//noinspection deprecation
return context.getResources().getColorStateList(colorId);
} else {
return context.getColorStateList(colorId);
}
}
}
public static ColorStateList getActionTextColorStateList(Context context, @ColorRes int colorId) {
final TypedValue value = new TypedValue();
context.getResources().getValue(colorId, value, true);
if (value.type >= TypedValue.TYPE_FIRST_COLOR_INT && value.type <= TypedValue.TYPE_LAST_COLOR_INT) {
return getActionTextStateList(context, value.data);
} else {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1) {
//noinspection deprecation
return context.getResources().getColorStateList(colorId);
} else {
return context.getColorStateList(colorId);
}
}
}
@Nullable
public static ColorStateList getColorStateList(int resId, Context context) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return context.getColorStateList(resId);
} else {
//noinspection deprecation
return context.getResources().getColorStateList(resId);
}
} catch (Resources.NotFoundException nfe) {
return null;
}
}