为了账号安全,请及时绑定邮箱和手机立即绑定

NestedScrollView 在以编程方式添加 TextView 时不滚动

NestedScrollView 在以编程方式添加 TextView 时不滚动

蝴蝶刀刀 2023-05-10 13:55:14
我在我的应用程序的一个屏幕上包括一个棋盘和棋子,我想在它下面添加一个可滚动的视图,以在用户移动时显示移动符号。我可以将文本添加到视图中,但无论我尝试什么,视图都不会滚动。我已经广泛研究了其他用户在这个问题上遇到的麻烦。我已经包括 fillViewport="true" 和 layout_height="wrap_content" 但这些解决方案都不适合我。我玩过 nestedscrollview 的边距无济于事,过去几天我一直遇到这个问题,但仍然没有弄清楚我做错了什么。这是 xml 文件:<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/chessBoardScreen"    android:layout_width="match_parent"    androi:layout_height="match_parent"    android:clickable="true"    android:focusable="true"    app:layout_behavior="@string/appbar_scrolling_view_behavior"    tools:context=".ChessBoardActivity"    tools:showIn="@layout/app_bar_chess_board">    <android.support.constraint.ConstraintLayout        android:id="@+id/cl_parent"        android:layout_width="wrap_content"        android:layout_height="350dp"        app:layout_constraintBottom_toTopOf="@+id/move_view"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toTopOf="parent">        <ImageView            android:id="@+id/board"            android:layout_width="wrap_content"            android:layout_height="329dp"            android:layout_marginTop="8dp"            app:layout_constraintEnd_toEndOf="parent"            app:layout_constraintStart_toStartOf="parent"            app:layout_constraintTop_toTopOf="parent"            app:srcCompat="@drawable/chess_board" />        </android.support.constraint.ConstraintLayout>    </android.support.constraint.ConstraintLayout>
查看完整描述

1 回答

?
潇潇雨雨

TA贡献1833条经验 获得超4个赞

编辑:

我刚刚注意到代码中的实际问题。您正在使用翻译手动设置位置,这样做不会更新父布局的高度,这就是滚动视图不会滚动的原因。您应该使用 LinearLayout,这样您就不必计算位置,如果有两列,您可以使用 GridLayout。


旧答案:调用scrollView.invalidate()以便 scrollview 可以适应变化。如果它不起作用,那么invalidate()也调用 ConstraintLayout。如果还是不行,那就也打电话requestLayout()。


 ml.addView(tv);

 ml.invalidate();

 NestedScrollView scrollView = findViewById(R.id.move_view);

 scrollView.invalidate();

 scrollView.requestLayout();


查看完整回答
反对 回复 2023-05-10
  • 1 回答
  • 0 关注
  • 120 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信