从xml文件创建drawable

时间:2013-01-19 23:06:56

标签: android dictionary layout drawable

我正在使用map并尝试从我的布局文件中创建标记为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();
}

1 个答案:

答案 0 :(得分:3)

Drawable.createFromXml不适用于将LayoutsView加载到drawable !! 看这个用法示例:

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

相关问题