3

사용자 지정 XML 특성을 사용하여 이러한 특성을 기반으로보기의 여백 및 크기를 설정할 수있는 사용자 지정보기를 작성 중이며, ConstraintLayout get은 사용자 정의 값에 따라 결정되는 여백과 크기입니다. 여백은 아무런 차이가 없으며 뷰는 부모 ConstraintLayout의 왼쪽 위 모퉁이에 머물러 있습니다. 무엇이 문제 일 수 있습니까? (이미지 멀리 화면의 경계에서 500PXs을해야한다)ConstraintLayout 어린이 마진 및 크기를 프로그래밍 방식으로 변경하기

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(); 
} 

결과 :

enter image description here

답변

3

나는 해결책을 발견 : 분명히 안드로이드 적절한 인수로 ConstraintSet.LEFTConstraintSet.RIGHT을지지 않습니다, 그리고해야 ConstraintSet.STARTConstraintSet.END으로 대체하십시오.

+1

네, 매우 비슷한 상황이었고 ConstraintSet.START 및 ConstraintSet에서만 잘 작동했습니다 .END –

+0

감사합니다. 내 생명을 살 렸습니다. –

관련 문제