【问题标题】:ScrollView doesn't scroll when its child view is scaled upScrollView 在其子视图放大时不滚动
【发布时间】:2022-11-23 11:23:23
【问题描述】:

我的ScrollView里面有一个LinearLayoutCompat

<ScrollView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toTopOf="@id/overview_holder"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.appcompat.widget.LinearLayoutCompat
            android:id="@+id/seats_viewHolder"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" />

    </ScrollView>

LinearLayoutCompat 完全适合 ScrollView;问题是,当我尝试以编程方式放大 LinearLayoutCompat 时,ScrollView 仍然没有开始滚动。我如何更新 ScrollView 以便它滚动并显示屏幕外的子视图部分?

【问题讨论】:

  • 嗨 Roozbeh,你能提供一些关于如何以编程方式扩展 LinearLayoutCompat 的线索或代码吗?

标签: android android-layout android-view


【解决方案1】:

由于我没有关于如何以编程方式扩展 LinearLayoutCompat 的更多详细信息,因此没有任何线索可以了解该部分发生了什么。

但是我注意到您将layout_widthlayout_height设置为0dp,这可能会更改为match_parentwrap_content

这是代码的快照,我以编程方式将 ImageView 添加到 LinearLayoutCompat 中以模拟放大并且可以滚动滚动视图

<ScrollView
        android:id="@+id/scrollview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.appcompat.widget.LinearLayoutCompat
            android:id="@+id/seats_viewHolder"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" />

</ScrollView>
override fun onCreate(savedInstanceState: Bundle) {
    val linearLayoutCompat = findViewById<LinearLayoutCompat>(R.id.seats_viewHolder)
    for (i in 0 .. 100) {
        val imageView = ImageView(this).apply {
            setImageResource(R.drawable.ic_launcher_background)
        }
        linearLayoutCompat.addView(imageView)
    }
} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多