【问题标题】:Onlayout method on custom layout extending RelativeLayout自定义布局扩展RelativeLayout的Onlayout方法
【发布时间】:2013-08-21 03:41:26
【问题描述】:

在扩展 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);
        }
    }
    }

【问题讨论】:

    标签: android android-relativelayout


    【解决方案1】:

    我在 this 演示文稿中解释了如何编写自定义布局(尤其是 FlowLayout,这就是你想要做的)

    Video available here.

    【讨论】:

    • 我还有一个问题,似乎与相对布局有关。 ImageViews 的第一个 fow 很好,除了最后一个被切断,之后应该在下一行的那些不会出现在那里。我用 lp.addRule(RelativeLayout.BELOW, idOfViewToTheLeft);但它似乎不起作用。我做错了什么,仅覆盖 onLayout 方法就足够了吗? @罗曼盖伊
    • @Romain Guy:非常感谢。优秀的教程。
    • 内容无法访问。 Parley 与糟糕的加载时间和糟糕的 UI 作斗争,Google Docs zipfile 没有完全加载。可能是我的问题,但我宁愿在 on StackOverflow 的帖子中得到问题的实际答案。
    猜你喜欢
    • 2013-07-25
    • 2013-06-10
    • 1970-01-01
    • 2020-06-14
    • 1970-01-01
    • 2010-12-02
    • 2014-11-13
    • 1970-01-01
    • 2013-08-09
    相关资源
    最近更新 更多