关于延长RelativeLayout的自定义布局Onlayout方法 [英] Onlayout method on custom layout extending RelativeLayout
问题描述
什么是正确的方式来覆盖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
这篇关于关于延长RelativeLayout的自定义布局Onlayout方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!