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

3

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

嗨,使用布局做这件事可以吗? - Zain
是的,如果您指的是约束布局。 - Balasubramanian
1个回答

3
你可以通过在一个高度与屏幕高度相匹配的容器中嵌套 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>

我们应该如何包含这个布局,约束值是多少? - Balasubramanian
1
@Balasubramanian 您可以使用它替换您的共享布局。我只是添加了一个内部的ConstraintLayout,以便我们可以使用app:layout_constraintHeight_percent相对于屏幕高度来控制其高度。您可以将“<!-- other views -->”替换为所有相同的视图,如instruction_list列表、try_button等。 - Zain
我的意思是,创建这个作为可重复使用的布局。当我尝试将其包含在另一个XML中时遇到了问题。 - Balasubramanian
列表视图的约束条件应该是什么?如果达到最大值,它会重叠上面的列表视图内容。它不能适配上下视图之间的空间。 - Balasubramanian
instruction_subtitle缺乏垂直约束...ListView与之绑定。 - Zain

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接