int count=group.getChildCount(); for(int i=0;i<count;i++){ View view=group.getChildAt(i); String className=view.getClass().getSimpleName(); if(view instanceof TableRow){ } else if(view instanceof LinearLayout){ } else { continue; } }
主要有两种方法,一种是用className来判断,比如:if(className.equals("Button"));
另一种是用instanceof来判断,网上查了不少地方,发现都是这种写法:if(view instanceof ViewGroup),经本人测试,发现并不好用,可能是android sdk版本的问题,我的是21,如果要用instanceof最好是像上面的代码那样写:if(view instanceof TableRow),即写具体的容器类名就可以了。