如何使用滚动视图?

时间:2019-04-16 05:18:31

标签: android user-interface scrollview

我想放置一个滚动视图以滚动屏幕上的内容,但是我不明白我在做什么错。任何帮助将不胜感激。我需要将约束布局放到线性布局中,然后在滚动视图中放入线性视图,还是在这里做其他错误?

 <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fillViewport="true">

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

            <android.support.constraint.ConstraintLayout
                android:id="@+id/layout_fragment_head"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:background="@color/color_theme_main_dark"
                app:layout_constraintBottom_toTopOf="@+id/guideline15"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">

                // I edited the code here for simplicity

            </android.support.constraint.ConstraintLayout>

            <fragment
                android:id="@+id/fragment_deviceslist"
                android:name="com.resatech.android.scoutandroid.master.fragments.DevicesListFragment"
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:defaultNavHost="true"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="@+id/guideline15"
                tools:layout="@layout/fragment_devices_list" />

            <android.support.constraint.Guideline
                android:id="@+id/guideline15"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                app:layout_constraintGuide_percent=".3" />


            </android.support.constraint.ConstraintLayout>

         </ScrollView>

2 个答案:

答案 0 :(得分:1)

您不应将ConstraintLayout放在ScrollView内。否则,ConstraintLayout中的对象将受到约束而无法滚动。

ScrollView放在ConstraintLayout内,然后,ScrollView内的所有对象都可以滚动(因为它们不受约束)。 在这种情况下,唯一受约束的视图是ScrollView,这是正确的。

如果您想要固定的标头,请将其放置在ScrollView约束的ConstraintLayout顶部。

答案 1 :(得分:0)

ScrollView

只需添加一层!
要解决您的问题,请像这样编辑代码:

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true">

        <RelativeLayout
           android:layout_width="match_parent"
           android:layout_height="match_parent">

                /* Your Code of Constraint Layout Like :
                <RelativeLayout />
                <ImageView />
                <LinerLayout/>
                and ....  */

        </RelativeLayout>

</ScrollView>
相关问题