Android获取activity的所有子view(使用到递归)
2018-12-07 本文已影响0人
指尖劃過諾言
//获取 activity中的所有view
private void getAllViews(Activity act) {
List<View> list = getAllChildViews(act.getWindow().getDecorView());
}
private List<View> getAllChildViews(View view) {
List<View> allchildren = new ArrayList<View>();
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;
}