3

ConstraintLayout子供の余白やサイズを変更し、これらの属性に基づいてビューの余白やサイズを設定する属性、すべてが正常に働いていました得意先の値によって決定されるマージンとサイズを取得します。 マージンは、任意の違いを確認しないと、ビューは、親ConstraintLayoutの左上隅にとどまります。 何が問題なのでしょうか?は、私は、カスタムXMLを使用できるようにカスタムビューを書いているプログラムで

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

結果(画像は離れて、画面の境界から500PXsでなければなりません):どうやらAndroidのは、適切な引数としてConstraintSet.LEFTConstraintSet.RIGHTを取らないと、次の条件を満たす必要があります。

enter image description here

答えて

3

私は解決策を見つけましたConstraintSet.STARTConstraintSet.ENDと置き換えることができます。

+1

はい、私は非常に似たような状況を持っていたし、それは –

+0

おかげで私の命を救っただけConstraintSet.STARTとConstraintSet.ENDでうまくいきました。 –

関連する問題