好吧,我最近开始使用Drawable tintedDrawable = getTintedIcon(
ContextCompat.getDrawable(context, R.drawable.ic_android),
ThemeUtils.darkTheme ? light : dark);
public static Drawable getTintedIcon(Drawable drawable, int color) {
if (drawable != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && drawable instanceof VectorDrawable) {
drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
}
drawable = DrawableCompat.wrap(drawable.mutate());
DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_IN);
DrawableCompat.setTint(drawable, color);
return drawable;
} else {
return null;
}
}
,我认为它们可以在所有Android版本中正常工作,但似乎它们会导致前棒棒糖设备崩溃。
问题是我用这段代码创建了一个有色的drawable:
imageView.setImageDrawable(tintedDrawable);
创建有色的drawable后,我使用以下方法设置:
app:srcCompat="@drawable/ic_android"
这在Lollipop和更新的Android版本中可以正常使用,但在旧版本中并没有。
根据Chris Banes' Medium post,我可以使用:
imageView.setImageResource(R.drawable.ic_android);
或
build.gradle
但是当我需要以编程方式对其进行着色时,我该怎么办?有人可以帮助和/或向我解释一下吗?
提前致谢。
我的android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
文件中已有此内容,以防万一:
public void Member() {
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
"PrivateDialog");
try {
mFriendsRelation = query.get(relationid).getRelation("Member");
} catch (ParseException e1) {
e1.printStackTrace();
}
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
mFriendsRelation.getQuery().findInBackground(new FindCallback<ParseUser>() {
@Override
public void done(List<ParseUser> friends, ParseException e) {
if (e == null) {
mFriends = friends;
if (member.getAdapter() == null) {
member.setAdapter(new MemberAdapter(member.getContext(), mFriends, R.layout.member_group_item));
member.setHasFixedSize(true);
member.setLayoutManager(new LinearLayoutManager(GroupDialogActivity.this));
member.setItemAnimator(new DefaultItemAnimator());
} else {
// refill the adapter!
Log.i("Refill", "todo");
}
} else {
Log.e("TAG", e.getMessage());
}
}
});
}
});
}
答案 0 :(得分:1)
尝试将此声明为ImageView:
android:textColor
但在此之前,如果您正在使用gradle v2.0 +,请将其添加到您的gradle:
app:srcCompat="@drawable/ic_iconname"/>
答案 1 :(得分:0)
不要这样做会有所帮助,但这是我使用的代码,并且尚未遇到任何问题。
public static boolean isAboveOrEqualAPILvl(int apiLvl) {
return Build.VERSION.SDK_INT >= apiLvl;
}
@TargetApi(Build.VERSION_CODES.M)
public static int getResourceColor(@NonNull Context context, @ColorRes int id) {
if (isAboveOrEqualAPILvl(Build.VERSION_CODES.M)) {
return context.getColor(id);
} else {
//noinspection deprecation
return context.getResources().getColor(id);
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Nullable
public static Drawable getIconDrawable(@NonNull Context context, @DrawableRes int drawableId,
int colorId, boolean isMutate) {
Drawable drawable;
if (isAboveOrEqualAPILvl(Build.VERSION_CODES.LOLLIPOP)) {
drawable = context.getDrawable(drawableId);
} else {
//noinspection deprecation
drawable = context.getResources().getDrawable(drawableId);
}
drawable = isMutate ? DrawableCompat.wrap(drawable).mutate() : DrawableCompat.wrap(drawable);
if (colorId>-2) {
DrawableCompat.setTint(drawable, getResourceColor(context, colorId));
}
return drawable;
}
public static void changeDrawableTint(@Nullable Context context, @NonNull Drawable drawable,
@ColorRes int colorId) {
if (context==null) { return; }
DrawableCompat.setTint(drawable, getResourceColor(context, colorId));
}