如何获取设置为 wrap_content 的约束布局高度?

时间:2021-05-16 16:45:11

标签: java android android-constraintlayout

如何获得约束布局高度的error_layout?我需要通过为下面的 result_layout 视图留出空间来为另一个布局 error_layout 设置边距底部。 我把它分成了两个布局,一个接一个地制作动画。

试过这个。但它给出了错误的计算。

private void getViewHeight(final ConstraintLayout layout) {
        layout.post(new Runnable() {
            @Override
            public void run() {
                layout_height_margin = layout.getHeight();
            }
        });
    }


在创建视图后调用 getViewHeight(error_layout)

constraintSet.connect(result_layout.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, layout_height_margin);
// Expecting the exact value of error_layout height to set margin value.

...

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#BEE">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/error_layout"
        android:layout_marginStart="12dp"
        android:layout_marginTop="12dp"
        android:layout_marginEnd="12dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="40dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        ...

    </androidx.constraintlayout.widget.ConstraintLayout>

     <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/result_layout"
        ...
     >
     </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

动画布局:

private void animateResult(ConstraintLayout constraint, Boolean show, int margin, int delay) {
        final ConstraintSet constraintSet = new ConstraintSet();
        constraintSet.clone(root_layout);
        constraintSet.clear(constraint.getId(), show ? ConstraintSet.TOP : constraintSet.BOTTOM);
        constraintSet.connect(constraint.getId(), show ? ConstraintSet.BOTTOM : ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, margin);

        final Transition transition = new ChangeBounds();
        transition.setInterpolator(new AnticipateOvershootInterpolator(1.0f));
        transition.setDuration(1200);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                TransitionManager.beginDelayedTransition(root_layout, transition);
                constraintSet.applyTo(root_layout);
            }
        }, delay);

    }

0 个答案:

没有答案