Android:为什么视图的ID应该是正数?
在 View.java 中,
setId - “设置此视图的标识符。标识符在此视图的层次结构中不必是唯一的。标识符应该是正数。”
&
findViewById - “查找具有给定 id 的子视图。如果该视图具有给定 id,
为什么我不能使用负数作为视图 id?我注释掉了系统
public final View findViewById(int id) {
// The original android check at here
// if (id < 0) {
// is commented out to test if system could run when Ids are negative numbers
if (id == NO_ID) {
return null;
}
return findViewTraversal(id);
}
似乎工作正常。
In View.java,
setId - "Sets the identifier for this view. The identifier does not have to be unique in this view's hierarchy. The identifier should be a positive number."
&
findViewById - "Look for a child view with the given id. If this view has the given id,
Why I could not use a negative number as the id of the view? I commented out the
public final View findViewById(int id) {
// The original android check at here
// if (id < 0) {
// is commented out to test if system could run when Ids are negative numbers
if (id == NO_ID) {
return null;
}
return findViewTraversal(id);
}
The system seems to be working fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这很可能只是一个设计决策。负标识符通常用于通知底层实体出现问题,因此使用负 ID 通常会引起不满。
It most likely is just a design decision. Negative identifiers are commonly used to notify that there's something wrong with the underlying entity, thus using negative id's is often frowned upon.