从xml文件创建drawable

从xml文件创建drawable

问题描述:

我正在使用地图并尝试从我的布局文件创建标记作为可绘制对象。 我需要我的标记视图来改变取决于点的类型,因此我宣布我的第一个图像为静态,我的第二个图像为动态(动态更改),但它不起作用。从xml文件创建drawable

我的布局文件

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    <ImageView 
    android:id="@+id/marker" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/marker" > 
    </ImageView> 

    <ImageView 
    android:id="@+id/category" 
    android:layout_width="10dp" 
    android:layout_height="10dp" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:src="@drawable/category_for_children" /> 
</RelativeLayout> 

我的代码:

Resources res = getResources(); 
try { 
    marker = Drawable.createFromXml(res, res.getXml(R.layout.marker_on_map)); 
} catch (NotFoundException e) { 
    e.printStackTrace(); 
} catch (XmlPullParserException e) { 
    e.printStackTrace(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

Drawable.createFromXml并非用于装载LayoutsView到绘制! 看到它的用法此样本:

http://spearhend.blogspot.com/2012/04/load-android-drawable-from-xml.html

+1

链路只有答案通常不是最佳的,因为链接可能成为死。你介意更新你的答案,总结你提供的解决方案吗? –