J'ai un listview à l'intérieur de la mise en page des contraintes. Lorsqu'il y a plus de listes, je veux restreindre la hauteur de la disposition parent. Comment puis-je définir la hauteur maximale de la mise en page de manière programmatique pour les éléments suivants ? bottom_layout
? Afficher un maximum de 80 % de la hauteur du dispositif.
Voici la disposition :
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/bottom_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<Button
android:id="@+id/try_again"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/primary_button"
android:minWidth="180dp"
android:text="@string/error_try_again"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<ListView
android:id="@+id/instruction_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@+id/try_again"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/instruction_subtitle" />
<TextView
android:id="@+id/instruction_subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/instruction_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/instruction_title"
app:layout_constraintBottom_toTopOf="@+id/instruction_subtitle"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
J'ai essayé cette solution en appelant onCreate
mais ça n'a pas marché :
private void BottomCardHeight(){
DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
float density = metrics.density;
float device_height = metrics.heightPixels / density;
bottom_layout = findViewById(R.id.bottom_layout);
ConstraintSet set = new ConstraintSet();
set.clone(bottom_layout);
set.constrainMaxHeight(R.id.bottom_layout, (int) (device_height * 0.8));
// set.constrainHeight(R.id.bottom_layout, (int) (device_height * 0.8)); both not working
set.applyTo(bottom_layout);
//ConstraintLayout mConstrainLayout = (ConstraintLayout) findViewById(R.id.bottom_layout);
//ConstraintLayout.LayoutParams lp = (ConstraintLayout.LayoutParams) mConstrainLayout.getLayoutParams();
//lp.matchConstraintMaxHeight = (int) (device_height * 0.8);
//mConstrainLayout.setLayoutParams(lp);
}