我在assets文件夾內有一個包含一些xml佈局(只是簡單形狀)的子文件夾。我的目標是能夠創建一個Drawable對象用作背景一個看法。目前它只是簡單的形狀,但最終會有很多。我不想使用資源區有幾個原因(沒有子文件夾,不需要硬編碼資源路徑等)。在Assets文件夾中創建xml文件中的Drawable
工作在圖像可繪製上的普通代碼返回null和xml文件。有沒有人有任何指導如何做到這一點?下面的代碼示例。
XML文件:用於讀取文件,非常類似於Java
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<stroke android:width="2dp" android:color="#c9c6af" />
<solid android:color="#ffffffff" />
</shape>
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;
}