Android 自定义布局上的Onlayout方法扩展RelativeLayout
在扩展RelativeLayout的自定义布局中,重写onLayout方法的正确方法是什么? 我试着把所有的视图放在一张桌子上。我们的想法是用ImageView填充一行,直到填满为止,然后继续在新行中操作Android 自定义布局上的Onlayout方法扩展RelativeLayout,android,android-relativelayout,Android,Android Relativelayout,在扩展RelativeLayout的自定义布局中,重写onLayout方法的正确方法是什么? 我试着把所有的视图放在一张桌子上。我们的想法是用ImageView填充一行,直到填满为止,然后继续在新行中操作 @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); int idOfView
@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);
}
}
}
@覆盖
仅受保护的void布局(布尔值已更改、int l、int t、int r、int b){
超级在线布局(更改,l,t,r,b);
int idOfViewToTheLeft=1;
RelativeLayout.LayoutParams params=新的RelativeLayout.LayoutParams(getContext(),);
图像视图书签;
对于(int counter=1;计数器1){
if(this.getWidth()>(bookmark.getLeft()+bookmark.getWidth()){
params.addRule(RelativeLayout.RIGHT_OF,bookmark.getId()-1);
}否则{
参数addRule(RelativeLayout.down,idOfViewToTheLeft);
idOfViewToTheLeft=bookmark.getId();
}
}
bookmark.setScaleType(ScaleType.CENTER_-CROP);
}
}
}
我解释了如何在演示文稿中编写自定义布局(尤其是FlowLayout,这似乎是您想要做的)
.我还有一个问题,似乎与相对布局有关。第一组ImageViews很好,除了最后一组被切断,之后的应该在下一行中不会出现。我使用了lp.addRule(RelativeLayout.down,idOfViewToTheLeft);但它似乎不起作用。我是否做错了什么,仅仅覆盖onLayout方法就足够了吗@罗曼Guy@Romain盖伊:非常感谢。非常好的教程。内容无法访问。Parley正在与糟糕的加载时间和糟糕的UI作斗争,Google文档zipfile没有完全加载。这可能是我这边的一个问题,但我更愿意在StackOverflow上的帖子中找到这个问题的实际答案。