在以编程方式将视图添加到层 次结构后,滚动ScrollView无法正常工作

问题描述 投票:-1回答:1

我以编程方式将视图添加到一个嵌入ScrollView的LinearLayout。但是ScrollView没有考虑扩展高度,因此滚动不起作用。添加视图后在ScrollView上调用invalidate()requestLayout()并没有解决问题。怎么做?

<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:fillViewport="true"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/pager">

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

        <LinearLayout
            android:id="@+id/layout_to_which_views_are_added"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/label"> 


services.groupBy { it.category }.forEach { category ->
                val view = layoutInflater.inflate(R.layout.service_category, rootLayout, false)
                view.findViewById<TextView>(R.id.lblHeader).text = getStringArray(R.array.service_category_headers)[category.key]
                view.findViewById<TextView>(R.id.txtServices).text = category.value.joinToString(", ") { it.display }
                layout_to_which_views_are_added.addView(view)
            }
scrollView.invalidate()
scrollView.requestLayout()
android android-scrollview
1个回答
0
投票

发现问题,ScrollView的父级布局,一个CoordinatorLayout,它本身是ConstraintLayout的父级,缺少bottomToBottom到父级的约束,它会将它拉伸到底部,然后使ScrollView可滚动到整个内容

© www.soinside.com 2019 - 2024. All rights reserved.