关于延长RelativeLayout的自定义布局Onlayout方法 [英] Onlayout method on custom layout extending RelativeLayout

查看:97
本文介绍了关于延长RelativeLayout的自定义布局Onlayout方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是正确的方式来覆盖onLayout方法延长RelativeLayout的自定义布局? 我试图将所有的意见在排序表中。我们的想法是,以填补一行与ImageViews,直到它的全部,然后继续在新行。

  @覆盖
保护无效onLayout(布尔改变,诠释L,INT T,INT R,int b)在{
        super.onLayout(改变,L,T,R,B);

        INT idOfViewToTheLeft = 1;

        RelativeLayout.LayoutParams PARAMS =新RelativeLayout.LayoutParams(的getContext());

        ImageView的书签;
        对于(INT计数器= 1;计数器< getChildCount();反++){
            书签=(ImageView的)findViewById(柜);
            如果(计数器→1){
               如果(this.getWidth()>(bookmark.getLeft()+ bookmark.getWidth())){
                   params.addRule(RelativeLayout.RIGHT_OF,bookmark.getId() -  1);
               } 其他 {
                   params.addRule(RelativeLayout.BELOW,idOfViewToTheLeft);
                   idOfViewToTheLeft = bookmark.getId();
               }
            }

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

解决方案

我将解释如何编写自定义布局(尤其是一FlowLayout中,这是你想要做什么似乎像)的<一个href="https://docs.google.com/a/google.com/leaf?id=0B_VKZCqEnHblNDdiY2UxODgtYWNhNS00MmU4LWE4NDMtZjQ1OWI5MDMxZTVh&sort=name&layout=list&num=50"相对=nofollow>这个 presentation

视频可以在这里找到

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);
        }
    }
    }

解决方案

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.

这篇关于关于延长RelativeLayout的自定义布局Onlayout方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆