Onlayout method on custom layout extending RelativeLayout

北慕城南 提交于 2019-12-03 08:51:11

问题


What is the proper way to override onLayout method in a custom layout extending the RelativeLayout? I'm trying to place all views in sort of a table. The idea is to fill one row with ImageViews until it's full and then continue in the new row.

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);

        int idOfViewToTheLeft = 1;

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(getContext(),);

        ImageView bookmark;
        for(int counter = 1; counter < getChildCount(); counter++) {
            bookmark = (ImageView) findViewById(counter);
            if(counter > 1) {
               if(this.getWidth() > (bookmark.getLeft() + bookmark.getWidth())) {
                   params.addRule(RelativeLayout.RIGHT_OF, bookmark.getId() - 1);
               } else {
                   params.addRule(RelativeLayout.BELOW, idOfViewToTheLeft);
                   idOfViewToTheLeft = bookmark.getId();
               }
            }

            bookmark.setScaleType(ScaleType.CENTER_CROP);
        }
    }
    }

回答1:


I explain how to write custom layouts (and in particular a FlowLayout, which is what you want to do it seems like) in this presentation

Video available here.



来源:https://stackoverflow.com/questions/4985760/onlayout-method-on-custom-layout-extending-relativelayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!