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

问题描述

我在约束布局中有一个列表视图。当它有更多列表时,想要限制父布局高度。如何以编程方式为 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>