我想将每个 ImageView 添加到数组列表中,无论 UI 中有多少个 ImageView (这样就可以在不更改代码的情况下更改它)。有没有办法做到这一点?像 findAllViews(ImageView)
方法这样的东西是完美的。
最佳答案
您可以使用以下方法迭代 xml 中的所有 View :
RelativeLayout yourLayout = (RelativeLayout)findViewById(R.id.layout);
for (int i = 0; i < yourLayout .getChildCount(); i++) {
View subView = yourLayout .getChildAt(i);
if (subView instanceof ImageView) {
ImageView imageView = (ImageView) subView;
//manipulate the imageView
}
}
关于xml - (Android) 如何找到 xml 文件中的所有 ImageView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32158877/