Constraint Layout measures incorrect height if it is nested into scrollview

冷暖自知 提交于 2019-12-23 09:38:36

问题


<android.support.v4.widget.NestedScrollView 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="wrap_content"
    android:fillViewport="true">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

///Content

    </android.support.constraint.ConstraintLayout>

</android.support.v4.widget.NestedScrollView>

In this case constraint layout to long. I also have problem with marginEnd of child views. I have a lot of child views similar to

 <TextView
            android:id="@+id/tvDurationPlan"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/margin_medium"
            android:textSize="@dimen/txt_size_small"
            style="@style/WhiteTextViewStyle"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintLeft_toLeftOf="@+id/tvPlanLabel"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/tvPlanLabel"
            app:layout_constraintVertical_bias="0.0"
            android:layout_marginEnd="16dp"
            tools:text="TextView" />

and layout_marginEnd doesn't work. Please help me!


回答1:


as I could say, this bug was fixed in com.android.support.constraint:constraint-layout:1.1.0-beta3




回答2:


To solve problem with marginEnd we have to use android:layout_width="0dp" for child views




回答3:


To solve this I get location of the last view and set this value to height of constraint layout

private void adjustConstraintLayoutToView(View view) {
    final ViewTreeObserver vto = view.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (mConstraintLayout.getMeasuredHeight() > 0) {
                int[] location = new int[2];
                view.getLocationOnScreen(location);
                mConstraintLayout.getLayoutParams().height = location[1];
                mConstraintLayout.requestLayout();
                if (vto.isAlive()) {
                    vto.removeOnGlobalLayoutListener(this);
                } else {
                    view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                }
            }
        }
    });
}


来源:https://stackoverflow.com/questions/43501646/constraint-layout-measures-incorrect-height-if-it-is-nested-into-scrollview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!