如果约束布局嵌套在scrollview

时间:2017-04-19 16:49:50

标签: android android-constraintlayout

<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>

在这种情况下,约束布局为long。我也有子视图的marginEnd问题。我有很多类似于

的子视图
 <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" />

和layout_marginEnd不起作用。 请帮帮我!

3 个答案:

答案 0 :(得分:1)

正如我所说,这个错误已在com.android.support.constraint中修复:约束布局:1.1.0-beta3

答案 1 :(得分:0)

要解决marginEnd的问题,我们必须使用android:layout_width =&#34; 0dp&#34;儿童观点

答案 2 :(得分:0)

要解决此问题,我获取最后一个视图的位置,并将此值设置为约束布局的高度

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);
                }
            }
        }
    });
}
相关问题