@Override
    public void onMeasure(int width, int height) {
        super.onMeasure(width, height);

        int totalWidth = 0;
        int totalHeight = 0;
        if (children.size() == 0) {
            totalHeight = 10; // TODO This is so bad, but if onMeasure is called before children are added...what can we do?
        }
        for (View child : children) {
            child.measure(child.getLayoutParameters().getWidth(), child.getLayoutParameters().getHeight());

            if (width == View.LayoutParameters.WRAP_CONTENT) {
                totalWidth += child.getMeasuredWidth();
            }
            if (height == View.LayoutParameters.WRAP_CONTENT) {
                if (child.getMeasuredHeight() > totalHeight) {
                    totalHeight = child.getMeasuredHeight();
                }
            }
        }

        int finalWidth = 0;
        if (width == View.LayoutParameters.WRAP_CONTENT) {
            finalWidth = totalWidth;
        } else {
            // Width must not be greater than the parent width
            if (width > getParentWidth()) {
                width = getParentWidth();
                Debug.log("Specified width was too large, changing to match parent.");
            }

            finalWidth = width;
        }

        int finalHeight = 0;
        if (height == View.LayoutParameters.WRAP_CONTENT) {
            finalHeight = totalHeight;
        } else {
            // Height must not be greater than the parent height
            if (height > getParentHeight()) {
                height = getParentHeight();
                Debug.log("Specified height was too large, changing to match parent for %s", getId());
            }

            finalHeight = height;
        }

        Debug.log("Setting in ViewGroup %s: Width=%s; Height=%s", getId(), finalWidth, finalHeight);
        setMeasuredDimension(finalWidth, finalHeight);
    }