下面列出了android.widget.TextView#getCompoundDrawablesRelative ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Calculates the real displayed with of a {@link TextView}. The measurement of text view
* (button) width is unreliable when inflating views, as it does not appear to take into account
* drawables or padding properly. This method calculates the drawn width manually instead,
* giving a reliable width that prevents character wrapping.
*
* @param view The text view to calculate the width for.
* @return A width in pixels.
*/
private static int calculateRealWidth(TextView view) {
// Calculate the padding of the view.
int realWidth = view.getCompoundDrawablePadding() + view.getTotalPaddingStart() + view.getTotalPaddingEnd();
// Add the width of the contained text.
String text = view.getTransformationMethod().getTransformation(view.getText(), view).toString();
realWidth += view.getPaint().measureText(text);
// Add the widths of any drawables on the button.
for (Drawable drawable : view.getCompoundDrawablesRelative()) {
if (drawable != null) realWidth += drawable.getIntrinsicWidth();
}
// Return the result.
return realWidth;
}
private void setAttr(final TextView view, final Integer color) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
if (color != null) {
view.setCompoundDrawableTintList(ColorStateList.valueOf(color));
}
}
else {
Drawable[] drawables = view.getCompoundDrawablesRelative();
for (Drawable drawable : drawables) {
if (drawable != null && color != null) {
DrawableCompat.setTint(drawable, color);
}
}
}
}
/**
* @see android.widget.TextView#getCompoundDrawablesRelative()
*/
public static Drawable[] getCompoundDrawablesRelative(TextView textView) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
return textView.getCompoundDrawablesRelative();
} else {
return textView.getCompoundDrawables();
}
}
public static Matcher<View> hasCompoundDrawableRelative(final boolean start,
final boolean top,
final boolean end,
final boolean bottom) {
return new TypeSafeMatcher<View>() {
@Override
protected boolean matchesSafely(View view) {
if (view instanceof TextView) {
TextView textView = (TextView) view;
Drawable[] drawables = textView.getCompoundDrawablesRelative();
boolean hasStart = drawables[0] != null;
boolean hastTop = drawables[1] != null;
boolean hasEnd = drawables[2] != null;
boolean hasBottom = drawables[3] != null;
return start == hasStart &&
top == hastTop &&
end == hasEnd &&
bottom == hasBottom;
}
return false;
}
@Override
public void describeTo(Description description) {
description.appendText("CompoundDrawables relative not matched");
}
};
}
protected void supportVectorDrawable(Context context, TextView view, AttributeSet attrs) {
Drawable[] drawablesCompat = MdVectorDrawableCompat.getFromAttribute(context, attrs,
R.attr.drawableLeftCompat, R.attr.drawableTopCompat, R.attr.drawableRightCompat, R.attr.drawableBottomCompat,
R.attr.drawableStartCompat, R.attr.drawableEndCompat);
boolean shouldInitiate = false;
for (Drawable drawable : drawablesCompat) {
if (drawable != null) {
shouldInitiate = true;
break;
}
}
if (shouldInitiate) {
Drawable[] drawables = view.getCompoundDrawables();
Drawable[] drawablesRelative;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
drawablesRelative = view.getCompoundDrawablesRelative();
} else {
drawablesRelative = drawables;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
view.setCompoundDrawablesRelativeWithIntrinsicBounds(
getDrawable(drawablesCompat[4] != null ? drawablesCompat[4] : drawablesCompat[0], drawablesRelative[0], drawables[0]),
getDrawable(drawablesCompat[1], drawablesRelative[1], drawables[1]),
getDrawable(drawablesCompat[5] != null ? drawablesCompat[5] : drawablesCompat[2], drawablesRelative[2], drawables[2]),
getDrawable(drawablesCompat[3], drawablesRelative[3], drawables[3])
);
} else {
view.setCompoundDrawablesWithIntrinsicBounds(
getDrawable(drawablesCompat[4] != null ? drawablesCompat[4] : drawablesCompat[0], drawablesRelative[0], drawables[0]),
getDrawable(drawablesCompat[1], drawablesRelative[1], drawables[1]),
getDrawable(drawablesCompat[5] != null ? drawablesCompat[5] : drawablesCompat[2], drawablesRelative[2], drawables[2]),
getDrawable(drawablesCompat[3], drawablesRelative[3], drawables[3])
);
}
}
}
public static void changeColorForDrawable(Context context, TextView textView, int color, PorterDuff.Mode mode) {
if (textView == null) { return; }
Drawable[] compoundDrawablesRelative = textView.getCompoundDrawablesRelative();
Drawable[] newDrawablesRelative = changeColorForDrawable(context, color, mode, compoundDrawablesRelative);
textView.setCompoundDrawablesRelative(newDrawablesRelative[0], newDrawablesRelative[1], newDrawablesRelative[2], newDrawablesRelative[3]);
}