Ich habe bereits einige Zeit damit verbracht, eine Lösung für dieses Problem zu finden.Android programmatische und XML-Einschränkungen sind unterschiedlich
In OnCreate Methode der Aktivität, ich zwei Schaltflächen erstellen und ihre Einschränkungen festlegen. Aber wenn dies in XML gemacht wird, sehen die gleichen Einschränkungen anders aus.
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:text="Button 1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="Button 2"
app:layout_constraintLeft_toRightOf="@+id/button"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Programmaticaly: Programmatic constraints image
Button btn1 = new Button(this);
Button btn2 = new Button(this);
btn1.setText("Button 1");
btn2.setText("Button 2");
layout.addView(btn1);
layout.addView(btn2);
ConstraintSet set = new ConstraintSet();
set.clone(layout);
set.connect(btn1.getId(), ConstraintSet.LEFT, layout.getId(), ConstraintSet.LEFT, 8);
set.connect(btn1.getId(), ConstraintSet.TOP, layout.getId(), ConstraintSet.TOP, 8);
set.connect(btn2.getId(), ConstraintSet.LEFT, btn1.getId(), ConstraintSet.RIGHT, 8);
set.connect(btn2.getId(), ConstraintSet.TOP, layout.getId(), ConstraintSet.TOP, 8);
set.connect(btn2.getId(), ConstraintSet.RIGHT, layout.getId(), ConstraintSet.RIGHT, 8);
set.applyTo(layout);
ich diesen Programmatically connecting multiple views set to any size using ConstraintLayout gelesen haben, aber das war nur eine falsche Verbindung und ich habe nichts falsch in meiner Verbindungen sehen, überprüft es mehrere mal.
thx, ich bin neu in der Programmierung von Android-Apps, aber ich habe einige Anwendungen programmiert, ohne Ansichten zur Laufzeit hinzuzufügen. hoffe, das sollte funktionieren. Ich werde es versuchen, sobald ich zu Hause bin –