2016-01-23 58 views
0

在一個特殊的android應用程序中,如果整個或部分父項溢出父窗口(窗口焦點已更改),我想從父項中刪除所有子視圖。刪除所有的父項溢出的子項

我已經嘗試編寫有關兒童和父寬度的手動計算,但我正在尋找一種面向對象的解決方案,以適用於各種視圖。

回答

0

這可能是最簡單的方法,只需擴展您正在使用的ViewGroup並在佈置佈局後刪除視圖。 (ViewGroups包括LinearLayoutRelativeLayout,...)

通過覆蓋onLayout你可以遍歷你的孩子和他們的父母之內刪除與邊界的任何意見並不。

注意事項:這可能會觸發另一個佈局調用,所以一定要妥善處理。

@Override 
protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 
    // call super to layout children and have their bounds set 
    super.onLayout(changed, left, top, right, bottom); 

    // Iterate over all children 
    for (int i = 0; i < getChildCount(); i++) { 
     View child = getChildAt(i); 
     if (child.getLeft() < left 
       // ... add top/bottom 
       || child.getRight() > right) { 
      removeView(child); 
     } 
    } 
} 

如果你想處理事情不延長ViewGroup你總是可以通過使用相同原理的觀點得到了layouted後處理事情。

相關問題