Изменение поля и размера для детей ConstraintLayout программно

Я пишу настраиваемое представление, чтобы иметь возможность использовать настраиваемые атрибуты XML для установки поля и размера представления на основе этих атрибутов, все работало нормально, пока я не дошел до той части, где дети ConstraintLayout получают свои поля и размер, определяемые пользовательскими значениями. Поля не имеют никакого значения, и представление остается в верхнем левом углу своего родительского ConstraintLayout. В чем может быть проблема? (Изображение должно быть на расстоянии 500 пикселей от границ экрана)

if (hasCustomWidth || hasCustomHeight || hasCustomTopMargin || hasCustomRightMargin || hasCustomBottomMargin || hasCustomLeftMargin) {
    if(((ViewGroup)getParent()) instanceof  LinearLayout) {
        LinearLayout.LayoutParams newLayoutParams = new LinearLayout.LayoutParams((int) Math.round(customWidth), (int) Math.round(customHeight));
        ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) newLayoutParams;
        marginLayoutParams.setMargins((int) Math.round(customLeftMargin), (int) Math.round(customTopMargin), (int) Math.round(customRightMargin), (int) Math.round(customBottomMargin));
        setLayoutParams(newLayoutParams);
    } else if(((ViewGroup)getParent()) instanceof ConstraintLayout) {
        ConstraintLayout parentConstraintLayout = (ConstraintLayout)CustomAppCompatImageView.this.getParent();

        ConstraintLayout.LayoutParams newLayoutParams = new ConstraintLayout.LayoutParams((int) Math.round(customWidth), (int) Math.round(customHeight));

        ConstraintSet constraintSet = new ConstraintSet();

        constraintSet.clone(parentConstraintLayout);

        constraintSet.connect(CustomAppCompatImageView.this.getId(), ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT, 500);
        constraintSet.setMargin(CustomAppCompatImageView.this.getId(), ConstraintSet.LEFT, 500);

        setLayoutParams(newLayoutParams);
        constraintSet.applyTo(parentConstraintLayout);

        parentConstraintLayout.invalidate();
    } else {
        throw new RuntimeException("no listed parent LayoutParams subclass!");
    }

    invalidate();
}

Результат:

1 ответ

Я нашел решение: видимо Android не берет ConstraintSet.LEFT а также ConstraintSet.RIGHT в качестве правильных аргументов, и должны быть заменены ConstraintSet.START а также ConstraintSet.END,

Другие вопросы по тегам