Android获取视图树
我想获取活动内部的所有视图。喜欢Android获取视图树,android,user-interface,view,Android,User Interface,View,我想获取活动内部的所有视图。喜欢 ArrayList<View> views = new Arraylist<>(); views.addAll(getAllViewsFromActivity()); 如何以编程方式在Android中获取此树?同时假设我得到了这棵树,我也能识别它们的类型吗,比如:view instanceof Button?你想要得到的视图是清晰的。因此,可以使用父视图(如LinerarLayout)获取子视图。所以你可以试试这个: int coun
ArrayList<View> views = new Arraylist<>();
views.addAll(getAllViewsFromActivity());
如何以编程方式在Android中获取此树?同时假设我得到了这棵树,我也能识别它们的类型吗,比如:view instanceof Button?你想要得到的视图是清晰的。因此,可以使用父视图(如LinerarLayout)获取子视图。所以你可以试试这个:
int count = parent.getChildCount();
for(int i =0; i< count;i++){
View child = parent.getChildAt(i);
//here you can to chage the view some proper.
//if you only want to change the background color, it belongs to view, don't
// need to cast.
}
int count=parent.getChildCount();
for(int i=0;i
它可以通过getChild获得,但它将是一个深循环。为什么你想得到它,也许我们应该改变去aim@Lenoarod我有一个糟糕的编码项目,我不想经历所有的设计。设计有3种颜色,黑白和灰色,我只想把颜色改成黑色。只是想设置相反的颜色。根据你的描述,我给你一个答案,你可以试试。
int count = parent.getChildCount();
for(int i =0; i< count;i++){
View child = parent.getChildAt(i);
//here you can to chage the view some proper.
//if you only want to change the background color, it belongs to view, don't
// need to cast.
}