【发布时间】:2016-09-12 23:45:41
【问题描述】:
我想从字符串中动态设置 ImageView 资源,但是我无法通过以下方法做到这一点。
我正在使用 Xamarin/Android 动态设置 ImageView 资源,如下所示;
ImageView homeItemImage = convertView.FindViewById<ImageView>(Resource.Id.homeItemImage);
int imageId = PlatformProvider.GetImageId(Context, contentTitle.IconImageUrl);
homeItemImage.SetImageResource(imageId);
GetImageId 函数:
public static int GetImageId(Context context, string imageName)
{
// int identifier = context.Resources.GetIdentifier(imageName, "drawable", context.PackageName);
int identifier = context.Resources.GetIdentifier(context.PackageName + ":drawable/" + imageName, null, context.PackageName);
return identifier;
}
我将 imageName 都传递为“Account.png”或“Account”都返回 int 为 0。
这是我的图片文件夹:
我将它们设置为 AndroidResource(这里显示的是 About.png 但 Account.png 是相同的方式):
它们以智能感知显示:
如何从字符串中设置图片来源?
【问题讨论】:
-
特别是this answer。
-
@Code-Apprentice 这个答案就是我要找的答案。这是 xamarin 方面的一个奇怪的实现。感谢您指出。