Android获取activity的所有子view(使用到递归)

 
  
//-----------------获取 activity中的所有view
    private void getAllViews(Activity act) {
        List list = getAllChildViews(act.getWindow().getDecorView());
    }

    private List getAllChildViews(View view) {
        List allchildren = new ArrayList();
        if (view instanceof ViewGroup) {
            ViewGroup vp = (ViewGroup) view;
            for (int i = 0; i < vp.getChildCount(); i++) {
                View viewchild = vp.getChildAt(i);
                allchildren.add(viewchild);
                //再次 调用本身(递归)
                allchildren.addAll(getAllChildViews(viewchild));
            }
        }
        return allchildren;
    }

你可能感兴趣的:(android,android,Activity)