如何在Android中使用AttributeSet(需要从Mapbox使用MapView)

时间:2014-07-09 19:55:25

标签: android mapbox

我想在我的Android应用程序中放置一个Mabpox MapView。 MapView应该从互联网获取磁贴,而不是从文件中获取磁贴。因此,我创建了一个mapbox帐户来获取地图框ID。

我现在卡在MapView上设置mapbox id,因为mapbox quick-start guide似乎已经过时了。 MapView构造函数如下所示:

public MapView(Context context, AttributeSet attributeSet)

我知道AttributeSet需要一个包含我的mapbox id的属性。因此我在/ res / values /:

中创建了一个名为attrs.xml的文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MapView">
        <attr name="mapid" format="string">brgr.myId</attr>
    </declare-styleable>
</resources>

出于测试目的,我创建了一个带有MainActivity的新应用程序模块,我现在只需将MapView设置为ContentView,以简化操作。

我现在的问题是Mapbox构造函数需要一个AttributeSet,但我不知道如何将attrs.xml文件转换为AttributeSet对象。

1 个答案:

答案 0 :(得分:1)

    mapView = (MapView) findViewById(R.id.mapview);
    XmlPullParser parser=getResources().getXml(R.xml.attrs);
    AttributeSet attrs = Xml.asAttributeSet(parser);
    mapView = new MapView(this, attrs);

此外,attrs.xml文件应位于res/xml/,而不是res/values/