计算View树上所有view的数量

public int getCount(ViewGroup viewGroup){

        int count=viewGroup.getChildCount();

        //循环获取子View
        for(int i=0;i<count;i++){
            View child=viewGroup.getChildAt(i);
            if(child instanceof ViewGroup){
                //如果子View是ViewGroup,则用递归获取子View数量
                int childCount = getCount((ViewGroup)child);
                count+=childCount;
            }else {
                count++;
            }
        }
        
        return count;
    }

多叉树的遍历,Android的View基本API的使用

发布了496 篇原创文章 · 获赞 88 · 访问量 72万+

猜你喜欢

转载自blog.csdn.net/yzpbright/article/details/104538462