public static void applyGravityToLine(LineDefinition line, ConfigDefinition config) {
    final List<ViewDefinition> views = line.getViews();
    final int viewCount = views.size();
    if (viewCount <= 0) {
      return;
    }

    float totalWeight = 0;
    for (int i = 0; i < viewCount; i++) {
      final ViewDefinition child = views.get(i);
      totalWeight += getWeight(child, config);
    }

    ViewDefinition lastChild = views.get(viewCount - 1);
    int excessLength =
        line.getLineLength()
            - (lastChild.getLength()
                + lastChild.getSpacingLength()
                + lastChild.getInlineStartLength());
    int excessOffset = 0;
    for (int i = 0; i < viewCount; i++) {
      final ViewDefinition child = views.get(i);
      float weight = getWeight(child, config);
      int gravity = getGravity(child, config);
      int extraLength;
      if (totalWeight == 0) {
        extraLength = excessLength / viewCount;
      } else {
        extraLength = Math.round(excessLength * weight / totalWeight);
      }

      final int childLength = child.getLength() + child.getSpacingLength();
      final int childThickness = child.getThickness() + child.getSpacingThickness();

      Rect container = new Rect();
      container.top = 0;
      container.left = excessOffset;
      container.right = childLength + extraLength + excessOffset;
      container.bottom = line.getLineThickness();

      Rect result = new Rect();
      Gravity.apply(gravity, childLength, childThickness, container, result);

      excessOffset += extraLength;
      child.setInlineStartLength(result.left + child.getInlineStartLength());
      child.setInlineStartThickness(result.top);
      child.setLength(result.width() - child.getSpacingLength());
      child.setThickness(result.height() - child.getSpacingThickness());
    }
  }