在约束布局上设置最大高度

Set max height on constraint layout

我在约束布局中有一个列表视图。当它有更多列表时,要限制父布局高度。如何以编程方式为 bottom_layout 设置最大布局高度?最多显示设备高度的 80%。

这是布局:

<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>

在调用 onCreate 时尝试了此解决方案,但没有成功:

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

您可以通过将 ConstraintLayout 包裹在另一个高度与屏幕高度匹配的布局中来实现这一点。

然后将以下约束添加到内部 ConstraintLayout 以获得最大高度为总屏幕高度的 80%。这需要有一个匹配约束的高度

android:layout_height="0dp"
app:layout_constraintHeight_max="wrap"
app:layout_constraintHeight_percent="0.8"

因此,外部布局为:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/bottom_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_max="wrap"
        app:layout_constraintHeight_percent="0.8"
        app:layout_constraintStart_toStartOf="parent">


        <!-- other views -->


    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>