Loop through all subviews of an Android view?

后端 未结 5 1399
北荒
北荒 2020-12-08 04:48

I’m working on a game for Android. To help implement it, my idea is to create a subclass of a view. I would then insert several instances of this class as children of the ma

5条回答
  •  攒了一身酷
    2020-12-08 05:34

    Try this. Takes all views inside a parent layout & returns an array list of views.

    public List getAllViews(ViewGroup layout){
            List views = new ArrayList<>();
            for(int i =0; i< layout.getChildCount(); i++){
                views.add(layout.getChildAt(i));
            }
            return views;
        }
    

提交回复
热议问题