2

在 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);
}

该系统似乎工作正常。

4

1 回答 1

5

这很可能只是一个设计决定。负标识符通常用于通知底层实体有问题,因此使用负 ID 通常不受欢迎。

于 2012-01-13T10:35:33.113 回答