将两个 ScrollView 包装成一个 LinearLayout

Wrapping two ScrollViews into a LinearLayout

我正在努力为 Android Studio (Kotlin) class 问题之一找到解决方案 Google:如何添加另一个与第一个 ScrollView 一起向下滚动的图像.我创建了一个 LinearLayout,其中有两个 ScrollView。第一个 ScrollView 有大量的文本,可以完美地向下滚动。但是,在第二个ScrollView中,对于文字上方的图片,并没有随着文字向下滚动。

这是 activity_main 的设计元素视图(星号下方的绿色条是第二个 ScrollView 中的 ImageView1):

这是我的 activity_main 中滚动视图的代码:

    <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ScrollView
        android:id="@+id/bio_scroll2"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:srcCompat="@android:drawable/button_onoff_indicator_on" />
    </ScrollView>

    <ScrollView
        android:id="@+id/bio_scroll"
        android:layout_width="match_parent"
        android:layout_height="463dp">

        <TextView
            android:id="@+id/bio_text"
            style="@style/NameStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:lineSpacingMultiplier="1.2"
            android:text="@string/bio" />
    </ScrollView>
</LinearLayout>

一些指导将不胜感激。

我告诉您只使用一个 ScrollView 的原因是您不需要两个。您可以在 Scrollview 中使用任意数量的视图,只需确保 ScrollView 的子视图是 Layout。您可以使用任何布局,然后将您拥有的任何内容放入该布局中。 ScrollView就是这么用的

作为 official docs of ScrollView 状态:

Scroll view may have only one direct child placed within it. To add multiple views within the scroll view, make the direct child you add a view group, for example LinearLayout, and place additional views within that LinearLayout.

因此,将您的代码更改为:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ScrollView
        android:id="@+id/bio_scroll2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:srcCompat="@android:drawable/button_onoff_indicator_on" />

            <TextView
                android:id="@+id/bio_text"
                style="@style/NameStyle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:lineSpacingMultiplier="1.2"
                android:text="@string/bio" />

        </LinearLayout>

    </ScrollView>

</LinearLayout>

此外,如果您不想在其中添加任何其他视图,您可以删除根 LinearLayout,这将使 ScrollView 成为根布局。

此外,height as match_parentScrollView 中不起作用,它仅适用于 wrap_content 或任何固定大小,所以如果你想制作它取其父级的全高,您必须在 ScrollView's 标签中使用 android:fillViewport="true" 并且其父级的高度应为 match_parent.