(Android) 如何在我的 xml 文件中找到所有 ImageView?
(Android) How can I find all ImageViews in my xml file?
我想将每个图像视图添加到一个数组列表中,无论 UI 中有多少(因此可以在不更改代码的情况下进行更改)。有没有办法做到这一点? findAllViews(ImageView)
之类的方法就完美了。
您可以使用以下方法遍历 xml 中的所有视图:
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
}
}
你可以这样做:
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
int childsNumber = relativeLayout.getChildCount();
其中 relativeLayout 是仅包含所有 ImageView 的布局。如果你有其他视图与你的 relativeLayout,你可以计算这些其他视图并减去它们。这是一个例子:
例如,如果您有带有 2 个 TextView 和 1 个 Button 的各种 ImageView,您将执行以下操作:
int childsNumber = relativeLayout.getChildCount() - 3;
其中 3 是非 ImageView 的总视图。
当然你可以使用任何其他类型的布局而不是RelativeLayout,只要按照我写的逻辑。希望对您有所帮助。
如果您正在考虑 Root
下的所有 ImageView
/**
* View is the root view
* i.e in case of activity just pass R.id.content
* or in case of Fragment pass it the reference you get from LayoutInflator
*/
List<View> listOfImgViews = new ArrayList<>();
private void viewIterator(View view){
if (view instanceof ViewGroup && !(view instanceof AdapterView)){
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++){
if(((ViewGroup) view).getChildAt(i) instanceof ImageView){
listOfImgViews.add(((ViewGroup) view).getChildAt(i));
}else if( ((ViewGroup) view).getChildAt(i) instanceof ViewGroup && !(((ViewGroup) view).getChildAt(i) instanceof AdapterView)){
viewIterator(((ViewGroup) view).getChildAt(i));
}
}
}
}
我想将每个图像视图添加到一个数组列表中,无论 UI 中有多少(因此可以在不更改代码的情况下进行更改)。有没有办法做到这一点? findAllViews(ImageView)
之类的方法就完美了。
您可以使用以下方法遍历 xml 中的所有视图:
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
}
}
你可以这样做:
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
int childsNumber = relativeLayout.getChildCount();
其中 relativeLayout 是仅包含所有 ImageView 的布局。如果你有其他视图与你的 relativeLayout,你可以计算这些其他视图并减去它们。这是一个例子:
例如,如果您有带有 2 个 TextView 和 1 个 Button 的各种 ImageView,您将执行以下操作:
int childsNumber = relativeLayout.getChildCount() - 3;
其中 3 是非 ImageView 的总视图。
当然你可以使用任何其他类型的布局而不是RelativeLayout,只要按照我写的逻辑。希望对您有所帮助。
如果您正在考虑 Root
下的所有 ImageView /**
* View is the root view
* i.e in case of activity just pass R.id.content
* or in case of Fragment pass it the reference you get from LayoutInflator
*/
List<View> listOfImgViews = new ArrayList<>();
private void viewIterator(View view){
if (view instanceof ViewGroup && !(view instanceof AdapterView)){
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++){
if(((ViewGroup) view).getChildAt(i) instanceof ImageView){
listOfImgViews.add(((ViewGroup) view).getChildAt(i));
}else if( ((ViewGroup) view).getChildAt(i) instanceof ViewGroup && !(((ViewGroup) view).getChildAt(i) instanceof AdapterView)){
viewIterator(((ViewGroup) view).getChildAt(i));
}
}
}
}