将视图添加到布局会覆盖视图 ID
我想知道是否有人对此进行实验,我有一个自定义视图,它绘制了雷达,一切都很好,绘制,测量,布局,但是当我构造它时,我用这样的整数设置视图ID,
Radar radar = new Radar(context);
radar.setId(RADAR_COMPONENT.hashCode());
LayoutParams lParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
lParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
this.addView( radar, lParams );
然后我添加其他视图而不设置 id...像这样
this.addView( getItemView(item), new LayoutParams(150, 100));
...
this.addView( getItemView(item), new LayoutParams(150, 100));
...
getItemView(item) 从 xml 返回一个没有 id 的膨胀视图,而 item 是保存视图信息的对象。我的问题是,当我添加此视图时,第一个视图雷达...丢失了 id,并且 id 为 -1,有人尝试过吗?
多谢
i want to know if anybody experiments this, i have a Custom View, who draws a Radar, everything is fine, the draw, measurement, layout, but when i construct it i set the view id with an integer like this,
Radar radar = new Radar(context);
radar.setId(RADAR_COMPONENT.hashCode());
LayoutParams lParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
lParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
this.addView( radar, lParams );
and later i add others views without set the id... like this
this.addView( getItemView(item), new LayoutParams(150, 100));
...
this.addView( getItemView(item), new LayoutParams(150, 100));
...
the getItemView(item) returns a inflated View from xml without id, and item is the object who holds the view info. My problem is when i add this views the fist view Radar... lost the id, and the id is -1, Anyone experiments this?
Thanks a lot
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不明白你在哪里设置 id,也不知道你为什么要这样做,你仍然可以通过 xml 布局添加自定义视图,方法是提供视图的完整类和包名称
I don't see where you are setting the id, also why are you doing this the hard way you can still add your custom views via xml layouts by giving the full class plus package name for the view