NestedScrollview和ConstraintLayout可以一起工作吗?

时间:2016-09-29 13:18:26

标签: android android-layout android-studio android-constraintlayout

我正在尝试新的ConstraintLayout,而且非常酷且简单。 我尝试在NestedScrollview里面的坐标布局中应用它,其中ConstraintLayoutNestedScrollView的父级。

但它没有奏效,所有观点都是垂直崩溃的。我该如何解决这个问题?

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize" />

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior">

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

            <Button
                android:text="Button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/button"
                android:layout_marginStart="16dp"
                app:layout_constraintLeft_toLeftOf="parent"
                android:layout_marginLeft="16dp"
                android:layout_marginTop="16dp"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintRight_toLeftOf="@+id/button2"
                android:layout_marginEnd="8dp"
                android:layout_marginRight="8dp" />

            <TextView
                android:text="TextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/textView2"
                android:layout_marginTop="16dp"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                android:layout_marginBottom="16dp"
                android:layout_marginStart="16dp"
                app:layout_constraintLeft_toLeftOf="parent"
                android:layout_marginLeft="16dp"
                android:layout_marginEnd="16dp"
                app:layout_constraintRight_toRightOf="parent"
                android:layout_marginRight="16dp" />

            <Button
                android:text="Button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/button2"
                app:layout_constraintTop_toTopOf="@+id/textView2"
                app:layout_constraintBottom_toBottomOf="@+id/textView2"
                android:layout_marginEnd="16dp"
                app:layout_constraintRight_toRightOf="parent"
                android:layout_marginRight="16dp" />

            <android.support.constraint.Guideline
                android:layout_width="411dp"
                android:layout_height="0dp"
                android:id="@+id/guideline"
                app:layout_constraintGuide_begin="367dp"
                android:orientation="horizontal"
                tools:layout_editor_absoluteY="367dp"
                tools:layout_editor_absoluteX="0dp" />

            <ProgressBar
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:id="@+id/progressBar2"
                app:layout_constraintTop_toTopOf="@+id/guideline"
                android:layout_marginStart="16dp"
                app:layout_constraintLeft_toLeftOf="parent"
                android:layout_marginLeft="16dp"
                android:layout_marginEnd="16dp"
                app:layout_constraintRight_toRightOf="parent"
                android:layout_marginRight="16dp"
                android:layout_marginTop="40dp"
                app:layout_constraintHorizontal_bias="0.25"
                android:max="100"
                android:progress="76" />
        </android.support.constraint.ConstraintLayout>
    </android.support.v4.widget.NestedScrollView>


</android.support.design.widget.CoordinatorLayout>

3 个答案:

答案 0 :(得分:1)

我可能来晚了,但是在NestedScrollView中添加android:fillViewport="true"应该可以解决此问题。

答案 1 :(得分:0)

根据此link,这已在constraint-layout 1.0.0-alpha2中修复(可与Android Studio 2.2 Preview 2一起使用)。

答案 2 :(得分:0)

您的约束布局会占用整个空间,因此嵌套的滚动视图将无法滚动。使用wrap_content作为宽度。

...
<android.support.constraint.ConstraintLayout
            android:orientation="vertical"
            android:layout_width="wrap_content" <---here
            android:layout_height="match_parent">
...