android 从 EditText 更改图标的颜色
android change color of an icon from EditText
我有以下 EditText
:
editText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_arrow_bottom_right_black_18dp, 0);
我正在尝试使用以下方法以编程方式更改 ic_arrow_bottom_right_black_18dp
的颜色:
protected void setEditTextDisabled(EditText editText) {
editText.setFocusable(false);
editText.setClickable(false);
editText.setEnabled(false);
editText.setTextColor(ContextCompat.getColor(getContext(), R.color.package_components_group_text_color));
if (editText.getTag(R.id.values_list_selected_ids) == null) {
if (editText.getTag(R.id.values_list_selected_ids) == null) {
editText.setFocusableInTouchMode(true);
Drawable[] d = editText.getCompoundDrawables();
if (d.length == 4) {
d[2].setColorFilter(ContextCompat.getColor(getContext(), R.color.package_components_group_text_color), PorterDuff.Mode.SRC_ATOP);
}
}
}
Drawable background = editText.getBackground();
if (background instanceof ShapeDrawable) {
// ((ShapeDrawable)background).getPaint().setStroke(2, getResources().getColor(R.color.package_components_group_text_color));
// ((ShapeDrawable)background).getPaint().setStroke(2, getResources().getColor(R.color.package_components_group_text_color));
} else if (background instanceof GradientDrawable) {
((GradientDrawable)background).setStroke(2, getResources().getColor(R.color.package_components_group_text_color));
} else if (background instanceof ColorDrawable) {
// ((ColorDrawable)background).setStroke(2, getResources().getColor(R.color.package_components_group_text_color));
}
}
protected void setEditTextEnabled(EditText editText) {
editText.setEnabled(true);
editText.setFocusable(true);
editText.setClickable(true);
editText.setTextColor(ContextCompat.getColor(getContext(), R.color.black));
if (editText.getTag(R.id.values_list_selected_ids) == null) {
editText.setFocusableInTouchMode(true);
Drawable[] d = editText.getCompoundDrawables();
if (d.length == 4) {
d[2].setColorFilter(ContextCompat.getColor(getContext(), R.color.black), PorterDuff.Mode.SRC_ATOP);
}
}
Drawable background = editText.getBackground();
if (background instanceof ShapeDrawable) {
// ((ShapeDrawable)background).getPaint().setColor(getResources().getColor(R.color.black));
} else if (background instanceof GradientDrawable) {
((GradientDrawable)background).setStroke(2, getResources().getColor(R.color.black));
} else if (background instanceof ColorDrawable) {
// ((ColorDrawable)background).setColor(getResources().getColor(R.color.black));
}
}
问题是当调用上述方法之一时,EditText
的可绘制右侧图标变得不可见或变白。
我附上了一张图片,右边是编辑文本的图标。
你可以通过这个改变颜色
EditText lineColorCode = (EditText) findViewById(R.id.line_color_code);
int color = Color.parseColor("#AE6118"); //The color u want
lineColorCode.setColorFilter(color);
或者你可以这样做
Drawable mDrawable = context.getResources().getDrawable(R.drawable.balloons);
mDrawable.setColorFilter(new
PorterDuffColorFilter(0xffff00,PorterDuff.Mode.MULTIPLY));
这是正确答案
Drawable ddd = getResources().getDrawable(R.drawable.ic_arrow_bottom_right_black_18dp);
Drawable drawable = DrawableCompat.wrap(ddd);
DrawableCompat.setTint(drawable, ContextCompat.getColor(getContext(), R.color.package_components_group_text_color));
editText.setCompoundDrawables(null, null, drawable, null);
您可以使用 Picasso 库更改图标颜色而不是背景颜色。如果您可以将图标作为 ImageView 获取,则可以这样做。 Picasso 是非常简单且功能最强大的库。
Picasso.with(this)
.load(R.drawable.your_icon)
.transform(new ColorTransformation(getResources().getColor(R.color.your_color)))
.into(imageViewIcon);
我有以下 EditText
:
editText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_arrow_bottom_right_black_18dp, 0);
我正在尝试使用以下方法以编程方式更改 ic_arrow_bottom_right_black_18dp
的颜色:
protected void setEditTextDisabled(EditText editText) {
editText.setFocusable(false);
editText.setClickable(false);
editText.setEnabled(false);
editText.setTextColor(ContextCompat.getColor(getContext(), R.color.package_components_group_text_color));
if (editText.getTag(R.id.values_list_selected_ids) == null) {
if (editText.getTag(R.id.values_list_selected_ids) == null) {
editText.setFocusableInTouchMode(true);
Drawable[] d = editText.getCompoundDrawables();
if (d.length == 4) {
d[2].setColorFilter(ContextCompat.getColor(getContext(), R.color.package_components_group_text_color), PorterDuff.Mode.SRC_ATOP);
}
}
}
Drawable background = editText.getBackground();
if (background instanceof ShapeDrawable) {
// ((ShapeDrawable)background).getPaint().setStroke(2, getResources().getColor(R.color.package_components_group_text_color));
// ((ShapeDrawable)background).getPaint().setStroke(2, getResources().getColor(R.color.package_components_group_text_color));
} else if (background instanceof GradientDrawable) {
((GradientDrawable)background).setStroke(2, getResources().getColor(R.color.package_components_group_text_color));
} else if (background instanceof ColorDrawable) {
// ((ColorDrawable)background).setStroke(2, getResources().getColor(R.color.package_components_group_text_color));
}
}
protected void setEditTextEnabled(EditText editText) {
editText.setEnabled(true);
editText.setFocusable(true);
editText.setClickable(true);
editText.setTextColor(ContextCompat.getColor(getContext(), R.color.black));
if (editText.getTag(R.id.values_list_selected_ids) == null) {
editText.setFocusableInTouchMode(true);
Drawable[] d = editText.getCompoundDrawables();
if (d.length == 4) {
d[2].setColorFilter(ContextCompat.getColor(getContext(), R.color.black), PorterDuff.Mode.SRC_ATOP);
}
}
Drawable background = editText.getBackground();
if (background instanceof ShapeDrawable) {
// ((ShapeDrawable)background).getPaint().setColor(getResources().getColor(R.color.black));
} else if (background instanceof GradientDrawable) {
((GradientDrawable)background).setStroke(2, getResources().getColor(R.color.black));
} else if (background instanceof ColorDrawable) {
// ((ColorDrawable)background).setColor(getResources().getColor(R.color.black));
}
}
问题是当调用上述方法之一时,EditText
的可绘制右侧图标变得不可见或变白。
我附上了一张图片
你可以通过这个改变颜色
EditText lineColorCode = (EditText) findViewById(R.id.line_color_code);
int color = Color.parseColor("#AE6118"); //The color u want
lineColorCode.setColorFilter(color);
或者你可以这样做
Drawable mDrawable = context.getResources().getDrawable(R.drawable.balloons);
mDrawable.setColorFilter(new
PorterDuffColorFilter(0xffff00,PorterDuff.Mode.MULTIPLY));
这是正确答案
Drawable ddd = getResources().getDrawable(R.drawable.ic_arrow_bottom_right_black_18dp);
Drawable drawable = DrawableCompat.wrap(ddd);
DrawableCompat.setTint(drawable, ContextCompat.getColor(getContext(), R.color.package_components_group_text_color));
editText.setCompoundDrawables(null, null, drawable, null);
您可以使用 Picasso 库更改图标颜色而不是背景颜色。如果您可以将图标作为 ImageView 获取,则可以这样做。 Picasso 是非常简单且功能最强大的库。
Picasso.with(this)
.load(R.drawable.your_icon)
.transform(new ColorTransformation(getResources().getColor(R.color.your_color)))
.into(imageViewIcon);