获取 activity中的所有子控件

 

	/** 获取 activity中的所有view */
	public static List Childs(Activity act)
	{
		View activityRoot = act.getWindow().getDecorView();
		List list = Childs(activityRoot, false);
		
		return list;
	}
	
	/** 获取当前View的所有子view */
	public static List Childs(View view, boolean ContainsThis)
	{
		List viewList = new ArrayList();
		
		if (!viewList.contains(view)) viewList.add(view);
		if (view instanceof ViewGroup)
		{
			ViewGroup group = (ViewGroup) view;
			for (int i = 0; i < group.getChildCount(); i++)
			{
				View child = group.getChildAt(i);
				if (!viewList.contains(child)) viewList.add(child);
				
				// 添加child的子节点
				List subList = Childs(child, true);
				for (View v : subList)
				{
					if (!viewList.contains(v)) viewList.addAll(subList);
				}
			}
		}
		
		if (!ContainsThis) viewList.remove(view);
		
		return viewList;
	}

 

你可能感兴趣的:(获取,activity中的所有子控件,java,android)