android中获得某个activity中的view

获得指定activity中的VIEW,通过代码的形式

public class GetActivityView {
	
	private Activity context;
	private View view;
	
	public GetActivityView(Activity context){
		this.context = context;
	}
	
	public List<View> getAllChildViews() {
		View view = context.getWindow().getDecorView();
		return getAllChildViews(view);
	}

	public 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);
				String name = viewchild.getClass().getSimpleName();
				System.out.println(name);
				allchildren.add(viewchild);
				allchildren.addAll(getAllChildViews(viewchild));
			}
		}
		return allchildren;
	}
	
	public List<View> getAllChildViewsByName(View view,String name) {
		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);
				String viewName = viewchild.getClass().getSimpleName();
				if(viewName.equals(name)){
					allchildren.add(viewchild);
					allchildren.addAll(getAllChildViews(viewchild));
				}
			}
		}
		return allchildren;
	}
	
}



版权声明:本文为lastTNT原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。