2015-04-06 74 views
0

所以,爲了實踐,我正在學習如何以編程方式創建視圖。我創建了一個擴展Viewgroup的新佈局(我稱之爲Custom1),它將子視圖(全部大小相同)放在兩列中。ViewGroup Errors

這個組的孩子們也是一個自定義佈局(我稱之爲Custom2),它包含一個imageview和兩個文字瀏覽。我使用for循環將必要數量的視圖添加到視圖組,並且onLayout被重寫。

現在,我試圖在選中「顯示佈局邊界」選項的情況下在Nexus 4上運行此操作。我可以看到Custom1的孩子的界限都在正確的位置,並且基於日誌,custom2的孩子也處於正確的位置。但是,只有第一個「custom2」正確顯示(即,第一個custom2顯示了一個imageView和兩個文本視圖,其餘都是空的)。

父視圖是否可能覆蓋子視圖?

如果沒有,有沒有人遇到過類似的問題?

下面是我的一些代碼CUSTOM1:

@Override 
protected void onLayout(boolean changed, int l, int t, int r, int b) { 
    final int count = getChildCount(); 
    if (ItemToDebug.equals("Layout")){ 
     if (count == numberOfChannels){ 
      Log.d("Layout:", "Number of children matches number of channels"); 
     }else{ 
      Log.d("Layout:", "Mismatch between number of children and number of channels"); 
     } 
     Log.d("Layout:", "onLayout " + Integer.toString(count) + " children"); 
    } 

    for (int i = 0; i < count; i++){ 
     View child = getChildAt(i); 
     if (i%2 == 0){ 
      if (LayoutLeftChild(i/2, l, t, r, b, child)){ //Lays out Child, returning a Boolean if successful 
       if (ItemToDebug.equals("Layout")){ 
        Log.d("Layout:", "onLayoutLeftChild number " + Integer.toString(i/2) + "successful"); 
       } 
      } else 
      if (ItemToDebug.equals("Layout")){ 
       Log.d("Layout:", "onLayoutLeftChild number " + Integer.toString(i/2) + "failed"); 
      } 
     } 
     if (i%2 == 1){ 
      if (LayoutRightChild(i/2, l, t, r, b, child)){ 
       if (ItemToDebug.equals("Layout")){ 
        Log.d("Layout:", "onLayoutRightChild number " + Integer.toString(i/2) + "successful"); 
       } 
      }else{ 
       if (ItemToDebug.equals("Layout")){ 
        Log.d("Layout:", "onLayoutRightChild number " + Integer.toString(i/2) + "failed"); 
       } 
      } 
     } 
    } 

} 


/* 
Left edge for right column = l + (r-l)/2 + 15 
Right edge for right column = r - 20 
20dp Margin between rows 
Rows are 400 dp tall 
*/ 
private boolean LayoutRightChild(int i, int l, int t, int r, int b, View child) { 
    final View Child = child; 

    final int Left = l + (r-l)/2 + 15; 
    final int Right = r - 20; 
    final int Top = t + i*20 + (i-1)*400; 
    final int Bottom = Top + 400; 

    Child.layout(Left, Top, Right, Bottom); 
    if (ItemToDebug.equals("Layout")){ 
     Log.d("Layout:", "Child laid out at (" + Integer.toString(Left) + ", " + Integer.toString(Top) + ", " + Integer.toString(Right) + ", " + Integer.toString(Bottom) + ")"); 
    } 

    return true; 
} 



/* 
Left edge for left column = l + 20 
Right edge for left column = l + (r-l)/2 - 15 
20dp Margin between rows 
Rows are 400 dp tall 
*/ 
private boolean LayoutLeftChild(int i, int l, int t, int r, int b, View child) { 
    final View Child = child; 

    final int Left = l + 20; 
    final int Right = l + (r-l)/2 - 15; 
    final int Top = t + i*20 + (i-1)*400; 
    final int Bottom = Top + 400; 

    Child.layout(Left, Top, Right, Bottom); 

    if (ItemToDebug.equals("Layout")){ 
     Log.d("Layout:", "Child laid out at (" + Integer.toString(Left) + ", " + Integer.toString(Top) + ", " + Integer.toString(Right) + ", " + Integer.toString(Bottom) + ")"); 
    } 
    return true; 
} 

下面是CUSTOM2一些代碼:

@Override 
protected void onLayout(boolean changed, int l, int t, int r, int b) { 

    final int count = getChildCount(); //There should be three children - one ImageView on top and two TextViews on bottom 
    if (ItemToDebug.equals("Layout")){ 
     if (count == 3){ 
      Log.d("View Contents:", "3 Children Views found"); 
     }else{ 
      Log.d("View Contents:", "Number of Children Incorrect. " + Integer.toString(count) + " children found."); 
     } 
     Log.d("Layout:", "onLayout " + Integer.toString(count) + " children"); 
    } 

    //Get children here in for loop and place. 
    for (int i = 0; i < count; i++){ 
     final View child = this.getChildAt(i); 

     //Layout should already have margins, so align contents with left and right sides. 
     int width = r - l; 
     int top; 
     int height; 
     switch(i){ 
      case 0: 
       top = t; 
       height = 100; 
       if (ItemToDebug.equals("Layout")) { 
        Log.d("Layout:", "Image Laid out at (" + Integer.toString(l) + ", " + Integer.toString(top) + ", " + Integer.toString(r) + ", " + Integer.toString(top + height) + ")"); 
       } 
       break; 
      case 1: 
       top = t + 100; 
       height = 60; 
       if (ItemToDebug.equals("Layout")) { 
        Log.d("Layout:", "TextView (nowPlaying) Laid out at (" + Integer.toString(l) + ", " + Integer.toString(top) + ", " + Integer.toString(r) + ", " + Integer.toString(top + height) + ")"); 
       } 
       break; 
      case 2: 
       top = t + 160; 
       height = 60; 
       if (ItemToDebug.equals("Layout")) { 
        Log.d("Layout:", "TextView (nextPlaying) Laid Out at (" + Integer.toString(l) + ", " + Integer.toString(top) + ", " + Integer.toString(r) + ", " + Integer.toString(top + height) + ")"); 
       } 
       break; 
      default: 
       top = t; 
       height = 0; 
       if (ItemToDebug.equals("Layout")){ 
        Log.d("Layout:", "More than 3 children have been added to the Custom2"); 
       } 
       break; 
     } 

     child.layout(l, top, r, top + height); 

    } 
} 

這是我的日誌:

https://drive.google.com/file/d/0B0EGM_1_9jazWEZ1RXh4a2VXdnM/view?usp=sharing

而屏幕截圖。

Screenshot

回答