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

标签 java android android-layout constraints android-constraintlayout

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

关于java - 在约束布局上设置最大高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67659501/

相关文章:

java - 创建并读取一次性创建的文件

java - 在 Flash 页面中,选择框(组合框)无法使用 Webdriver 工作

java - 提取封装在对象中的数组

android - 如何设置 Eclipse 的构建配置以生成正确的、可调试的 NDK 应用程序?

android - 将 View 粘贴到 ScrollView 的底部

android - 可绘制为 CardView 的背景

android - 不同 API 版本之间的 UI 兼容性问题

java - 如何使用彩色按钮作为用户选择从第二个 Activity 更改我的 MainActivity 的背景颜色?

java - 将 JPanel 类放入另一个类的 JFrame 中

iphone - 跨平台应用