extends android.view.ViewGroup两种实现

/*    private int measureHeight(int heightMeasureSpec) {
        int count = getChildCount();
        int rowCount = 1;
        int left = 0;
        int top = 0;
        int right = left + getMeasuredWidth();
        int childHeight = (count == 0) ? 0 : getChildAt(0).getMeasuredHeight();
        int childWidth;
        int totalWidth = left;
        int totalHeight = top;
        // 最后两个child是EditText和ImageButton
        int editWidth = getChildAt(count - 2).getMeasuredWidth();
        int buttonWidth = getChildAt(count - 1).getMeasuredWidth();
        for (int i = 0; i < count - 2; i++) {
            childWidth = getChildAt(i).getMeasuredWidth();
           
            // 加上当前childView超出右边界换行。
            if (totalWidth + childWidth > right) {
                rowCount++;
                totalWidth = left;
            }
            totalWidth += childWidth;
           
            getChildAt(i).setLeft(totalWidth - childWidth);
            getChildAt(i).setRight(totalWidth);
            getChildAt(i).setTop(childHeight * (rowCount - 1));
            getChildAt(i).setBottom(childHeight * rowCount);
        }
       
        // 加上最后两个childView超过右边界换行。
        if (totalWidth + editWidth + buttonWidth > right) {
            rowCount++;
            totalWidth = left;
        }
        totalWidth += editWidth + buttonWidth;
       
        // 倒数第2个childView:收件人编辑框EditText。
        getChildAt(count - 2).setLeft(totalWidth - editWidth - buttonWidth);
        getChildAt(count - 2).setRight(totalWidth - buttonWidth);
        getChildAt(count - 2).setTop(childHeight * (rowCount - 1));
        getChildAt(count - 2).setBottom(childHeight * rowCount);
       
        // 倒数第1个childView:addContact按钮。
        getChildAt(count - 1).setLeft(right - buttonWidth);
        getChildAt(count - 1).setRight(right);
        getChildAt(count - 1).setTop(childHeight * (rowCount - 1));
        getChildAt(count - 1).setBottom(childHeight * rowCount);
       
        totalHeight += childHeight * rowCount;
       
        return totalHeight;
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        View v = null;
        for (int i = 0; i < getChildCount(); i++) {
            v = getChildAt(i);
            v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
        }
        if(changed) {
            layout(0, 0, getMeasuredWidth(), getMeasuredHeight());
        }
    }*/

 


private int measureHeight(int heightMeasureSpec) {
    int count = getChildCount();
    int rowCount = 1;
    int left = 0;
    int top = 0;
    int right = left + getMeasuredWidth();
    int childHeight = (count == 0) ? 0 : getChildAt(0).getMeasuredHeight();
    int childWidth;
    int totalWidth = left;
    // 最后两个child是EditText和ImageButton
    int editWidth = getChildAt(count - 2).getMeasuredWidth();
    int buttonWidth = getChildAt(count - 1).getMeasuredWidth();
    for (int i = 0; i < count - 2; i++) {
        childWidth = getChildAt(i).getMeasuredWidth();
       
        // 加上当前childView超出右边界换行。
        if (totalWidth + childWidth > right) {
            rowCount++;
            totalWidth = left;
        }
        totalWidth += childWidth;
    }
   
    // 加上最后两个childView超过右边界换行。
    if (totalWidth + editWidth + buttonWidth > right) {
        rowCount++;
        totalWidth = left;
    }
    totalWidth += editWidth + buttonWidth;
    return top + childHeight * rowCount;
}

protected void onLayout(boolean changed, int l, int t, int r, int b) {
    int count = getChildCount();
    int rowCount = 1;
    int left = 0;
    int right = left + getMeasuredWidth();
    int childHeight = (count == 0) ? 0 : getChildAt(0).getMeasuredHeight();
    int childWidth;
    int totalWidth = left;
    int totalHeight = childHeight;
    // 最后两个child是EditText和ImageButton
    int editWidth = getChildAt(count - 2).getMeasuredWidth();
    int editHeight = getChildAt(count - 2).getMeasuredHeight();
    int buttonWidth = getChildAt(count - 1).getMeasuredWidth();
    View view = null;
    for (int i = 0; i < count - 2; i++) {
        view = getChildAt(i);
        childWidth = view.getMeasuredWidth();
       
        // 加上当前childView超出右边界换行。
        if (totalWidth + childWidth > right) {
            rowCount++;
            totalHeight += childHeight;
            totalWidth = left;
        }
        totalWidth += childWidth;
        view.layout(totalWidth - childWidth, childHeight * (rowCount - 1),
                totalWidth, childHeight * rowCount);
    }
   
    // 加上最后两个childView超过右边界换行。
    if (totalWidth + editWidth + buttonWidth > right) {
        rowCount++;
        totalHeight += childHeight;
        totalWidth = left;
    }
    totalWidth += editWidth + buttonWidth;
    view = getChildAt(count - 2);
    view.layout(totalWidth - editWidth - buttonWidth,
            totalHeight - childHeight + (childHeight - editHeight)/2,
            totalWidth - buttonWidth, totalHeight);
    view = getChildAt(count - 1);
    view.layout(right - buttonWidth, totalHeight - childHeight,
            right, totalHeight);
    if(changed) {
        layout(0, 0, getMeasuredWidth(), getMeasuredHeight());
    }
}


public class RecipientEditor extends ViewGroup {

    public RecipientEditor(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        for (int i = 0; i < getChildCount(); i++) {
            getChildAt(i).measure(MeasureSpec.UNSPECIFIED,
                    MeasureSpec.UNSPECIFIED);
        }
        int measuredWidth = MeasureSpec.getSize(widthMeasureSpec);
        setMeasuredDimension(measuredWidth, measureHeight(heightMeasureSpec));
    }

posted on 2013-07-11 16:56  勤修  阅读(1593)  评论(0编辑  收藏  举报

导航