ScrollView Inside ConstraintLayout

时间:2018-01-05 08:25:44

标签: android android-layout android-scrollview

我想在ConstraintLayout中嵌入一个小的ScrollView。但是当我这样做时,ScrollView中的所有视图都会消失。

代码如下: 我有一个ConstraintLayout。在它内部是一个带有垂直LinearLayout的ScrollView。这包含5个ImageViews。

<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:background="#000000"
android:orientation="horizontal"
android:baselineAligned="false"
android:id="@+id/cameraView">

<ScrollView
    android:id="@+id/thumbnails"
    android:layout_width="125dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"

    android:layout_weight="0.3"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/img1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            app:srcCompat="@mipmap/left" />

        <ImageView
            android:id="@+id/img2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            app:srcCompat="@mipmap/leftoblique" />

        <ImageView
            android:id="@+id/img3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            app:srcCompat="@mipmap/front" />

        <ImageView
            android:id="@+id/img4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            app:srcCompat="@mipmap/right_obl_initial" />

        <ImageView
            android:id="@+id/img5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            app:srcCompat="@mipmap/right" />
    </LinearLayout>
</ScrollView>
   .
   .
<!-- Other Views -->
   .
   .
   .
</android.support.constraint.ConstraintLayout>

我该如何解决这个问题?或者有其他方式吗?

1 个答案:

答案 0 :(得分:0)

尝试这种方式,将滚动视图放在约束布局

之外
<ScrollView
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/thumbnails"
    android:layout_width="125dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"

    android:layout_weight="0.3"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

<android.support.constraint.ConstraintLayout 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="horizontal"
android:baselineAligned="false"
android:id="@+id/cameraView">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/img1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            app:srcCompat="@mipmap/left" />

        <ImageView
            android:id="@+id/img2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            app:srcCompat="@mipmap/leftoblique" />

        <ImageView
            android:id="@+id/img3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            app:srcCompat="@mipmap/front" />

        <ImageView
            android:id="@+id/img4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            app:srcCompat="@mipmap/right_obl_initial" />

        <ImageView
            android:id="@+id/img5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            app:srcCompat="@mipmap/right" />
    </LinearLayout>

   .
   .
<!-- Other Views -->
   .
   .
   .
</android.support.constraint.ConstraintLayout>
</ScrollView>