2011-02-13 36 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