我在assets文件夹内有一个包含一些xml布局(只是简单形状)的子文件夹。我的目标是能够创建一个Drawable对象用作背景一个看法。目前它只是简单的形状,但最终会有很多。我不想使用资源区有几个原因(没有子文件夹,不需要硬编码资源路径等)。在Assets文件夹中创建xml文件中的Drawable
工作在图像可绘制上的普通代码返回null和xml文件。有没有人有任何指导如何做到这一点?下面的代码示例。
XML文件:用于读取文件,非常类似于Java
C#代码。是的,我知道我需要关闭InputStream:
public static Drawable GetDrawableFromAsset(Context context, string filePath)
{
AssetManager assetManager = context.Assets;
Stream istr;
Drawable drawable = null;
try
{
istr = assetManager.Open(filePath);
drawable = Drawable.CreateFromStream(istr, null);
}
catch (IOException e)
{
// handle exception
}
return drawable;
}
2017-05-01
Nigel DH