【问题标题】:Can NestedScrollview and ConstraintLayout work together?NestedScrollview 和 ConstraintLayout 可以一起工作吗?
【发布时间】:2017-02-07 20:19:42
【问题描述】:

我正在尝试新的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>

【问题讨论】:

  • 发布您的设计代码。
  • mmmm,给我 200 万,我已经删除了布局。我会重新制作视图
  • 已发布!他们都是垂直折叠的

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


【解决方案1】:

我可能会迟到,但在 NestedScrollView 中添加 android:fillViewport="true"应该可以解决问题。

【讨论】:

    【解决方案2】:

    根据这个link,这已经在constraint-layout 1.0.0-alpha2中修复了(Android Studio 2.2 Preview 2可用)。

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-11-11
        • 2013-07-09
        • 2016-01-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多