9

我有一個大約有12/13個字段的表單。我在約束佈局中使用了Scrollview。以下是XML佈局的層次結構。問題是,它不滾動到底部,而是滾動到第一個最初的10個視圖。最後3個字段被隱藏,因爲視圖不再滾動。約束佈局中的滾動查看不滾動到父約束的底部

父佈局的

<android.support.constraint.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:id="@+id/activity_register" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:focusableInTouchMode="true" 
android:orientation="vertical"> 

<!-- Textview and a button --> 

    <ScrollView 
    android:id="@+id/scrollView" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="10dp" 
    android:layout_marginTop="10dp" 
    android:orientation="vertical" 
    android:overScrollMode="never" 
    android:scrollbars="none" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
    app:layout_constraintTop_toBottomOf="@+id/view" 
    tools:layout_constraintBottom_creator="1" 
    tools:layout_constraintLeft_creator="1" 
    tools:layout_constraintRight_creator="1" 
    tools:layout_constraintTop_creator="1" 
    tools:layout_editor_absoluteX="0dp" 
    tools:layout_editor_absoluteY="0dp"> 


    <android.support.constraint.ConstraintLayout 
     android:id="@+id/constraintLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

       <!-- Child Views (12/13 views of the fields)--> 

    </android.support.constraint.ConstraintLayout> 

</ScrollView> 

</android.support.constraint.ConstraintLayout> 
+0

嘗試將'ScrollView'的'android:layout_height'參數更改爲'match_parent'。 – Ircover

+1

嘗試過,但它不起作用。 –

+0

設置高度以匹配parent和fillViewPort爲您的滾動視圖爲true,或嘗試嵌套SCrollview –

回答

11

這種佈局工作在我的應用程序。 關鍵是要設置滾動型這兩個屬性: 機器人:layout_height = 「0dp」 應用:layout_constraintBottom_toBottomOf = 「父」

從我的應用程序的簡化佈局:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.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:theme="@style/ThemeOverlay.AppCompat.Light"> 

    <RelativeLayout 
     android:id="@+id/linear" 
     android:layout_width="0dp" 
     android:layout_height="56dp" 
     android:background="@color/title" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" /> 

    <ScrollView 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toBottomOf="@id/linear"> 

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

      <TextView 
       android:id="@+id/titleView" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="8dp" 
       android:layout_marginStart="8dp" 
       android:text="@string/title" 
       android:textSize="14sp" 
       app:layout_constraintBaseline_toBaselineOf="@+id/title" 
       app:layout_constraintLeft_toLeftOf="parent" /> 

      <EditText 
       android:id="@+id/title" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_marginEnd="8dp" 
       android:layout_marginRight="8dp" 
       android:hint="toilet title" 
       android:inputType="text" 
       android:textColor="@android:color/holo_red_dark" 
       android:textSize="12sp" 
       app:layout_constraintLeft_toLeftOf="@+id/open_hour" 
       app:layout_constraintLeft_toRightOf="@+id/titleView" 
       app:layout_constraintRight_toRightOf="parent" 
       app:layout_constraintTop_toTopOf="parent" /> 
      ... 
      Other Views in ScrollView 
      ... 
     </android.support.constraint.ConstraintLayout> 
    </ScrollView> 
</android.support.constraint.ConstraintLayout> 
2

兩步

用於滾動視圖
  1. 保持佈局高度零
    android:layout_height="0dp"

  2. 再次爲滾動視圖
    android:fillViewport="true"

0

嘗試增加底部約束滾動型(如:app:layout_constraintBottom_toBottomOf="parent") 和改變機器人:layout_height = 「WRAP_CONTENT」 到android:layout_height="0dp"

1

在工作我的情況NestedScrollView而不是ScrollView。以下是我的工作佈局的片段:

<android.support.constraint.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"> 

    <!-- Some Views Here --> 

    <android.support.v4.widget.NestedScrollView 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:fillViewport="true" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toBottomOf="@+id/view"> 

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

      <!-- Some Views That can be Scrolled Here --> 

     </android.support.constraint.ConstraintLayout> 

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

</android.support.constraint.ConstraintLayout>