2011-02-13 62 views
4

在扩展RelativeLayout的自定义布局中重写onLayout方法的正确方法是什么? 我试图把所有的意见放在一个表中。这个想法是用ImageView填充一行,直到它满了,然后继续在新行中。扩展自定义布局的Onlayout方法RelativeLayout

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

回答

9

我将解释如何编写自定义布局(和特别是FlowLayout中,这是你想做的事,好像什么)在this呈现

Video available here

+0

谢谢,该教程示例(代码)真的很有帮助! – lpandzic 2011-02-14 12:59:23