首页 \ 问答 \ 如何在Android中使用AttributeSet(需要从Mapbox使用MapView)(How to use AttributeSet in Android (needed to use MapView from Mapbox))

如何在Android中使用AttributeSet(需要从Mapbox使用MapView)(How to use AttributeSet in Android (needed to use MapView from Mapbox))

我想在我的Android应用程序中放置一个Mabpox MapView。 MapView应该从Internet获取切片,而不是从文件中获取切片。 因此,我创建了一个mapbox帐户来获取mapbox ID。

我现在卡在MapView上设置mapbox id,因为mapbox快速入门指南似乎已经过时了。 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对象。


I would like to put a Mabpox MapView in my android application. The MapView should get the tiles from the internet, not from a file. Therefore I created a mapbox account to get a mapbox id.

I am now stuck setting the mapbox id on the MapView, because the mapbox quick-start guide seems to be out of date. The MapView constructor looks like this:

public MapView(Context context, AttributeSet attributeSet)

I understand that the AttributeSet wants an attribute containing my mapbox id. Therefore I created a file called attrs.xml inside /res/values/ :

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MapView">
        <attr name="mapid" format="string">brgr.myId</attr>
    </declare-styleable>
</resources>

For testing purposes, I have created a new application module with a MainActivity, where I would simply set the MapView as the ContentView for now, for making things easier.

My problem now is that the Mapbox constructor needs an AttributeSet, but I don't know how to convert the attrs.xml file into an AttributeSet object.


原文:https://stackoverflow.com/questions/24662705
更新时间:2020-03-26 12:10

最满意答案

    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/


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

Also, the attrs.xml file should be in res/xml/, not in res/values/.

2014-07-20

相关文章

更多

最新问答

更多
  • css在元素之前中断列而不破坏包装器(css break column before element without breaking the wrapper)
  • 如何在Xamarin共享项目中使用自定义渲染器(How to use Custom Renderer in Xamarin Shared Project)
  • 如何为特定表中的特定字段设置唯一?(How to set unique for specific field from specific table?)
  • Google SDK iOS - sign()方法完成处理程序(Google SDK iOS - sign() method completion handler)
  • 在具有接口{}值的地图上实现String()(Implement String() on a map with interface{} values)
  • 检查数据库中是否已存在用户名(Check if username already exist in DB)
  • 使用javascript进行ajax调用时阻止用户交互(Block user interaction while doing ajax call using javascript)
  • 什么'if(err)'在Javascript中精确测试?(What does 'if (err)' tests precisely in Javascript?)
  • jQuery mouseleave无法正常工作(jQuery mouseleave not working)
  • 寻求使用的一些说明(Seeking some clarification on use of )
  • 将数组传递给注释的语法(syntax for passing array to annotation)
  • 用于从两个日期范围之间的文件中提取数据的Shell脚本(Shell script to extract data from file between two date ranges)
  • 元素隐藏但父()没有(Element hides but parent() not)
  • 如何使用Google App Engine Java平台开发web ui(How to develop web ui with Google App Engine Java platform)
  • 对于OWL A级;(For an OWL class A; Getting all properties that A is their domain)
  • Excel VBA公式格式问题(Excel VBA Formula Format Issue)
  • ORA - 02287序列号不允许在这里(ORA - 02287 sequence number not allowed here)
  • Github拉忽略特定文件(Github Pull Ignore Specific File)
  • SQL CONVERT函数在SQL Server中工作但不在应用程序中(SQL CONVERT function working in SQL Server but not in application)
  • backbone.js适用于大型应用程序(backbone.js for large applications)
  • 防止程序关闭(Preventing program from closing)
  • 生成不带图像的heightMap(Generating a heightMap without an Image)
  • Bootstrap - 如何将包含文本的div居中?(Bootstrap - How to center div that has text inside it?)
  • Android - 片段findViewById()总是null?(Android - Fragment findViewById() always null?)
  • 确定CSS中的高度(Figuring out heights in CSS)
  • 使用__autoload包含类和使用命名空间(Use __autoload to include class and use namespace)
  • setTimeout()不允许我传递文本值[重复](setTimeout() doesn't allow me to pass text values [duplicate])
  • 在NSUserDefault中恢复值(Restoring value in NSUserDefault)
  • 知道如何将这种下沉的悬停效果添加到图像/链接吗?(Any idea how to add this sinking hover effect to an image/link?)
  • 在XIB中淡入/淡出UISegmentedControl(fade in/fade out UISegmentedControl in XIB)