Android 使用现有视图对象膨胀视图
我有一个自定义视图,我想从资源模板创建它。我的自定义视图构造函数接受设置为自定义视图的附加信息的附加参数。
问题是,当我膨胀视图时,我得到的视图对象不是自定义视图的子类,因为膨胀方法是静态的,并返回通用的新视图而不是自定义视图的实例。
我正在寻找一种通过传递我的自定义视图对象引用来膨胀视图的方法。
public class MLBalloonOverlayView extends View { MiscInfo mMiscInfo; public MLBalloonOverlayView(Context context, MiscInfo miscInfo) { super(context); mMiscInfo = miscInfo; } public View create(final int resource, final OverlayItem item, MapView mapView, final int markerID) { ViewGroup viewGroup = null; View balloon = View.inflate(getContext(), resource, viewGroup); // I want to return this object so later I can use its mMiscInfo //return this; return balloon; } }
I have a custom view that I would like to create from a resource template. My custom view constructor accepts additional parameters that are set as additional info for the custom view.
Problem is when I inflate the view I get a view object that is not subclassed from from custom view since the inflate method is static and returns a generic new view instead of an instance of my custom view.
Would I am looking for is a way to inflate the view by passing it my custom view object reference.
public class MLBalloonOverlayView extends View { MiscInfo mMiscInfo; public MLBalloonOverlayView(Context context, MiscInfo miscInfo) { super(context); mMiscInfo = miscInfo; } public View create(final int resource, final OverlayItem item, MapView mapView, final int markerID) { ViewGroup viewGroup = null; View balloon = View.inflate(getContext(), resource, viewGroup); // I want to return this object so later I can use its mMiscInfo //return this; return balloon; } }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看代码后 https://github.com/galex/android-mapviewballoons
我能够相应地更新我的代码。这个想法是您从资源创建一个布局,然后将膨胀的视图添加到扩展布局的类的实例中(如上面 Marcos 所建议的那样)。
After looking at code at https://github.com/galex/android-mapviewballoons
I was able to update my code accordingly. The idea being you create a layout from the resource and then you add the inflated view to the instance of a class that extends a layout (as Marcos suggested above).
将其充气到您的物体上。
Inflate it on your object.