下面列出了android.content.res.Resources#getColorStateList() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void scheduleTextColor(TV view, TVC call) {
Resources res = view.getResources();
if (mTextColorHighlightEnvRes != null) {
int textColorHighlight = res.getColor(mTextColorHighlightEnvRes.getResid());
call.scheduleHighlightColor(textColorHighlight);
}
if (mTextColorEnvRes != null) {
ColorStateList textColor = res.getColorStateList(mTextColorEnvRes.getResid());
call.scheduleTextColor(textColor);
}
if (mTextColorHintEnvRes != null) {
ColorStateList textColorHint = res.getColorStateList(mTextColorHintEnvRes.getResid());
call.scheduleHintTextColor(textColorHint);
}
if (mTextColorLinkEnvRes != null) {
ColorStateList textColorLink = res.getColorStateList(mTextColorLinkEnvRes.getResid());
call.scheduleLinkTextColor(textColorLink);
}
}
@Nullable
public static ColorStateList getColorStateList(@NonNull Resources res, @ColorRes int id, @Nullable Theme theme) throws NotFoundException {
if (VERSION.SDK_INT >= 23) {
return ResourcesCompatApi23.getColorStateList(res, id, theme);
}
return res.getColorStateList(id);
}
@TargetApi(23)
private void testGetColorStateList() {
ColorStateList csl = getColorStateList(R.color.text_selector1);
if (csl != null) {
mTestBtn.setTextColor(csl);
}
Resources resources = getResources();
ColorStateList csl2 = resources.getColorStateList(R.color.text_selector2);
if (csl2 != null) {
mTestBtn2.setTextColor(csl2);
}
}
@Override
public void apply(View view, String resName) {
if (TextUtils.isEmpty(resName)) return;
Resources mResources = view.getResources();
int resId = mResources.getIdentifier(resName, DEFTYPE_COLOR, view.getContext().getPackageName());
if (0 != resId) {
ColorStateList colorList = mResources.getColorStateList(resId);
if (colorList == null) return;
((TextView) view).setTextColor(colorList);
}
}
private static void endFont(SpannableStringBuilder text) {
int len = text.length();
Object obj = getLast(text, Font.class);
int where = text.getSpanStart(obj);
text.removeSpan(obj);
if (where != len) {
Font f = (Font) obj;
if (!TextUtils.isEmpty(f.mColor)) {
if (f.mColor.startsWith("@")) {
Resources res = Resources.getSystem();
String name = f.mColor.substring(1);
int colorRes = res.getIdentifier(name, "color", "android");
if (colorRes != 0) {
ColorStateList colors = res.getColorStateList(colorRes);
text.setSpan(new TextAppearanceSpan(null, 0, 0, colors,
null), where, len,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
} else {
int c = getHtmlColor(f.mColor);
if (c != -1) {
text.setSpan(new ForegroundColorSpan(c | 0xFF000000),
where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
if (f.mFace != null) {
text.setSpan(new TypefaceSpan(f.mFace), where, len,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
/**
* @see android.content.res.Resources#getColorStateList(int id).
*/
@SuppressWarnings("deprecation")
public static ColorStateList getColorStateList(Resources res, int id) throws NotFoundException {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return res.getColorStateList(id, null);
} else {
return res.getColorStateList(id);
}
}
@SuppressWarnings("deprecation")
public static ColorStateList getColorStateList(Resources resources, int id) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
return resources.getColorStateList(id);
} else {
return CompatibilityImpl.getColorStateList(resources, id);
}
}
public static ColorStateList getColorStateList(int colorStateId, Theme theme) {
Resources resources = NoHttp.getContext().getResources();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
return resources.getColorStateList(colorStateId, theme);
else
return resources.getColorStateList(colorStateId);
}
@Override
public ColorStateList getColorStateList(String resName) {
ColorStateList colorStateList = null;
try {
Resources res = context.getResources();
int resId = res.getIdentifier(resName, "color", currentPackageName);
colorStateList = res.getColorStateList(resId);
} catch (NotFoundException e) {
if (Logger.isDebug()) Log.e(TAG, e.getMessage(), e);
}
return colorStateList;
}
@TargetApi(Build.VERSION_CODES.M)
public static ColorStateList getColorStateList(Resources resources, int id) {
return resources.getColorStateList(id, null);
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public FloatingActionButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setFocusable(true);
setClickable(true);
if(Build.VERSION.SDK_INT > 14 && Build.VERSION.SDK_INT < 16){
setLayerType(LAYER_TYPE_SOFTWARE, null);
}
Resources r = context.getResources();
mInnerCircleOffset = r.getDimensionPixelSize(R.dimen.fab_inner_circle_offset);
mActionSize = r.getDimensionPixelSize(R.dimen.fab_action_icon_size);
mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mCirclePaint.setStyle(Paint.Style.FILL);
// Resolving attribute styles
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.FloatingActionButton);
mCirclePaint.setColor(array.getColor(R.styleable.FloatingActionButton_actionColor, Color.WHITE));
mActionIcon = array.getDrawable(R.styleable.FloatingActionButton_actionIcon);
if(mActionIcon != null){
mActionIcon.setBounds(0, 0, mActionSize, mActionSize);
}
array.recycle();
mCircle = new Circle();
mCirclePath = new Path();
ViewUtils.setBackground(this, r.getDrawable(R.drawable.floatingactionbutton_shadow_layer));
mRippleDrawable = new RippleDrawable(r.getColorStateList(R.color.overlay_color), null);
mRippleDrawable.setVisible(true, false);
mRippleDrawable.setCallback(this);
mRippleDrawable.setState(getDrawableState());
setOnTouchListener(new TouchTracker());
}
private void init(AttributeSet attrs, int defStyleAttr, int defStyleRes) {
if (attrs == null)
return;
// Get the super padding top
mTruePaddingTop = super.getPaddingTop();
final Context context = getContext();
final Resources resources = getResources();
TypedArray a = context.obtainStyledAttributes(
attrs, R.styleable.EditText, defStyleAttr, defStyleRes);
String fontFile = a.getString(R.styleable.EditText_gFont);
int lineSize = a.getDimensionPixelSize(R.styleable.EditText_gLineSize, resources.getDimensionPixelSize(R.dimen.g_editText_lineSize));
ColorStateList lineColor = a.getColorStateList(R.styleable.EditText_gLineColor);
// Set HintProperty
int titleModel = a.getInt(R.styleable.EditText_gHintTitle, 1);
int titleTextSize = a.getDimensionPixelSize(R.styleable.EditText_gHintTitleTextSize, resources.getDimensionPixelSize(R.dimen.g_editText_hintTitleTextSize));
int titlePaddingTop = a.getDimensionPixelSize(R.styleable.EditText_gHintTitlePaddingTop, 0);
int titlePaddingBottom = a.getDimensionPixelSize(R.styleable.EditText_gHintTitlePaddingBottom, 0);
int titlePaddingLeft = a.getDimensionPixelSize(R.styleable.EditText_gHintTitlePaddingLeft, 0);
int titlePaddingRight = a.getDimensionPixelSize(R.styleable.EditText_gHintTitlePaddingRight, 0);
a.recycle();
// Init color
if (lineColor == null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
lineColor = resources.getColorStateList(R.color.g_default_edit_view_line, null);
} else {
//noinspection deprecation
lineColor = resources.getColorStateList(R.color.g_default_edit_view_line);
}
}
if (!Ui.isHaveAttribute(attrs, "textColorHint") || getHintTextColors() == null) {
ColorStateList hintColor;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
hintColor = resources.getColorStateList(R.color.g_default_edit_view_hint, null);
} else {
//noinspection deprecation
hintColor = resources.getColorStateList(R.color.g_default_edit_view_hint);
}
setHintTextColor(hintColor);
}
// Set same values
setLineSize(lineSize);
setLineColor(lineColor);
setHintTitleTextSize(titleTextSize);
setHintTitleModel(titleModel);
// check for IDE preview render
if (!this.isInEditMode()) {
// Set Font
if (fontFile != null && fontFile.length() > 0) {
Typeface typeface = Ui.getFont(context, fontFile);
if (typeface != null) setTypeface(typeface);
}
}
// Init background and title
if (!Ui.isHaveAttribute(attrs, "background")) {
initBackground();
}
initHintTitleText();
// SetHintPadding
setHintTitlePadding(titlePaddingLeft, titlePaddingTop, titlePaddingRight, titlePaddingBottom);
}
private void init(AttributeSet attrs, int defStyleAttr, int defStyleRes) {
final Context context = getContext();
final Resources resource = getResources();
final float density = resource.getDisplayMetrics().density;
final int baseSize = (int) (density * 2);
if (attrs == null) {
mMarkDrawable = new CircleCheckDrawable(resource.getColorStateList(R.color.g_default_check_box));
setButtonDrawable(mMarkDrawable);
return;
}
// Load attributes
final TypedArray a = context.obtainStyledAttributes(
attrs, R.styleable.CheckBox, defStyleAttr, defStyleRes);
int borderSize = a.getDimensionPixelOffset(R.styleable.CheckBox_gBorderSize, baseSize);
int intervalSize = a.getDimensionPixelOffset(R.styleable.CheckBox_gIntervalSize, baseSize);
int markSize = a.getDimensionPixelOffset(R.styleable.CheckBox_gMarkSize, -1);
ColorStateList color = a.getColorStateList(R.styleable.CheckBox_gMarkColor);
String fontFile = a.getString(R.styleable.CheckBox_gFont);
a.recycle();
if (color == null)
color = resource.getColorStateList(R.color.g_default_check_box);
boolean isCustom = true;
if (markSize < 0) {
markSize = (int) (density * 22);
isCustom = false;
}
mMarkDrawable = new CircleCheckDrawable(color);
mMarkDrawable.setBorderSize(borderSize);
mMarkDrawable.setIntervalSize(intervalSize);
mMarkDrawable.setMarkSize(markSize, isCustom);
mMarkDrawable.inEditMode(isInEditMode());
setButtonDrawable(mMarkDrawable);
// Check for IDE preview render
if (!this.isInEditMode()) {
// Font
if (fontFile != null && fontFile.length() > 0) {
Typeface typeface = Ui.getFont(getContext(), fontFile);
if (typeface != null) setTypeface(typeface);
}
}
}
/**
* Returns a themed color state list associated with a particular resource
* ID. The resource may contain either a single raw color value or a
* complex {@link ColorStateList} holding multiple possible colors.
*
* @param resources Resources
* @param id The desired resource identifier of a {@link ColorStateList},
* as generated by the aapt tool. This integer encodes the
* package, type, and resource entry. The value 0 is an invalid
* identifier.
* @return A themed ColorStateList object containing either a single solid
* color or multiple colors that can be selected based on a state.
*/
public static ColorStateList getColorStateList(Resources resources, int id) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return resources.getColorStateList(id, null);
} else {
return resources.getColorStateList(id);
}
}