【问题标题】:How do i use a scrollview?我如何使用滚动视图?
【发布时间】:2019-04-16 05:18:31
【问题描述】:

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

 <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>

【问题讨论】:

标签: android user-interface scrollview


【解决方案1】:

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

ScrollView放在ConstraintLayout里面,那么ScrollView里面的所有对象都可以滚动(因为它们没有被约束)。 在这种情况下,唯一受约束的视图是ScrollView,这是正确的。

如果您想要一个固定的标题,请将其放在ScrollView 之外,并限制在ConstraintLayout 的顶部。

【讨论】:

  • 如果我这样做.. 它只是给出错误 Scrollview 只能包含一个直接的孩子。
  • 是的,ScrollView 应该只包含ScrollView 之外的片段和标头。当然,ScrollView 之外的GuideLine 也是。
【解决方案2】:

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>

【讨论】:

  • 你应该更多地解释你的答案 - 为什么你不能在滚动视图中添加更多布局?
  • 那么你是不是建议我应该把我的约束布局放在那个相对布局中?
  • @kkarakk body of scrollview 只能支持一个孩子以避免中断!您可以在子布局中编写其他布局
  • @MuhammadAbdullah 不!你只需要将你的第一个 ConstraintLayout 更改为 RelativeLayout
  • @hmotamed 我做到了,现在我得到了一个空白布局?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-09
  • 1970-01-01
  • 2012-03-10
  • 1970-01-01
  • 1970-01-01
  • 2021-12-21
相关资源
最近更新 更多