下面列出了android.widget.Button#getCompoundDrawables ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static Matcher<View> withCompoundDrawable(final int resourceId) {
return new BoundedMatcher<View, Button>(Button.class) {
@Override
public void describeTo(Description description) {
description.appendText("has compound drawable resource " + resourceId);
}
@Override
public boolean matchesSafely(Button textView) {
for (Drawable drawable : textView.getCompoundDrawables()) {
if (sameBitmap(textView.getContext(), drawable, resourceId)) {
return true;
}
}
return false;
}
};
}
public static void setTint(@NonNull Button button, @ColorInt int color) {
ColorStateList s1 = ColorStateList.valueOf(color);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
button.setCompoundDrawableTintList(s1);
} else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD_MR1) {
Drawable drawable = DrawableCompat.wrap(button.getCompoundDrawables()[1]);
button.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
DrawableCompat.setTintList(drawable, s1);
} else {
PorterDuff.Mode mode = PorterDuff.Mode.SRC_IN;
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
mode = PorterDuff.Mode.MULTIPLY;
}
if (button.getCompoundDrawables()[1] != null)
button.getCompoundDrawables()[1].setColorFilter(color, mode);
}
}
public ButtonProperty(Button button) {
width = button.getWidth();
height = button.getHeight();
text = button.getText().toString();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
drawables = button.getCompoundDrawablesRelative();
} else {
drawables = button.getCompoundDrawables();
}
}
protected void prepareButtonImage(Button b)
{
for (Drawable d : b.getCompoundDrawables())
{
CompatUtils.setDrawableColorAttr(getContext(), d,
b.isEnabled() ? R.attr.colorDialogContent : R.attr.colorDialogDisabledElement);
}
}
private static void colorizeButtonCompoundDrawable(int color, @NonNull Button button)
{
Drawable[] drawables = button.getCompoundDrawables();
for (Drawable d : drawables) {
if (d != null) {
d.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
}
}
button.setCompoundDrawables(drawables[0], drawables[1], drawables[2], drawables[3]);
}