自定义布局上的Onlayout方法扩展RelativeLayout

lpa*_*zic 4 android relativelayout

在扩展RelativeLayout的自定义布局中覆盖onLayout方法的正确方法是什么?我试图将所有视图放在一个表格中.我们的想法是用ImageViews填充一行直到它已满,然后继续在新行中.

@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);
        }
    }
    }
Run Code Online (Sandbox Code Playgroud)

Rom*_*Guy 9

我在这个演示文稿中解释如何编写自定义布局(特别是一个FlowLayout,这是你想要做的事情)

视频在这里提供.

  • 内容无法访问.Parley正在努力应对糟糕的加载时间和糟糕的用户界面,Google Docs zipfile无法完全加载.可能是我的问题,但我宁愿在*StackOverflow上的帖子*中得到问题的实际答案. (3认同)