2017-12-27 32 views
0

私はAndroidでアプリケーションを開発していますが、ボタンの配列に問題があります。私はConstraintLayoutを持って、動的に制約を(私は、ボタンの配置の3例を持っている)を設定する必要があります。ボタンの制約の設定をプログラムで変更し、スペースを埋める

<android.support.constraint.ConstraintLayout 
    android:id="@+id/bottomLayout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/border" 
    android:padding="3dp" 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
    app:layout_constraintTop_toBottomOf="@+id/middleLayoutText" 
    app:layout_constraintVertical_chainStyle="packed" > 

    <Button 
     android:id="@+id/firstButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <Button 
     android:id="@+id/secondButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

</android.support.constraint.ConstraintLayout> 

あなたはそれが2つのボタンが含まれている見ることができるように、今プログラムで私は自分の位置を設定する必要があり、例えば1で最初は2番目の行になります。

ConstraintLayout layout = (ConstraintLayout) constraintLayout.findViewById(R.id.bottomLayout); 
ConstraintSet set = new ConstraintSet(); 
set.clone(layout); 

Button firstButton = (Button) layout.findViewById(R.id.firstButton); 
firstButton.setText(buttonsOption.getFirstButton().getContent().getText()); 

Button secondButton = (Button) layout.findViewById(R.id.secondButton); 
secondButton.setText(buttonsOption.getSecondButton().getContent().getText()); 

set.connect(firstButton.getId(), ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT); 
set.connect(firstButton.getId(), ConstraintSet.RIGHT, secondButton.getId(), ConstraintSet.LEFT); 
set.connect(secondButton.getId(), ConstraintSet.START, firstButton.getId(), ConstraintSet.END); 
set.connect(secondButton.getId(), ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END); 
set.setHorizontalChainStyle(firstButton.getId(),ConstraintSet.CHAIN_PACKED); 
set.setHorizontalChainStyle(secondButton.getId(),ConstraintSet.CHAIN_PACKED); 
set.applyTo(layout); 

は、今では次のようになります:私はこのコードでそれをやっているimageが、私は最初のものは右に左2つ目のスペースの一部を埋めるために彼の幅を調整することを実現したいです。 layout_widthの組み合わせを試してみましたが、すべての接続を設定しましたが、まだ成功していません。

ご協力いただきありがとうございます。

答えて

0

は、あなたはまた、ConstraintSetドキュメントhereをチェックアウトする必要があります

set.constrainWidth(firstButton.getId(), ConstraintSet.MATCH_CONSTRAINT); 
set.constrainWidth(secondButton.getId(), ConstraintSet.MATCH_CONSTRAINT); 

を使用してみました。

+0

ありがとうございました!私はそれについて読んだが、私はそれをチェックすることを忘れて...それは素晴らしい作品:D –

関連する問題