gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:15:52 30 4
gpt4 key购买 nike

我在应用程序的一个屏幕上添加了一个棋盘和棋子,并且我想在其下方添加一个可 ScrollView ,以在用户移动时显示移动符号。我可以将文本添加到 View 中,但无论我如何尝试, View 都不会滚动。

我广泛研究了其他用户在这个问题上遇到的麻烦。我已经包含了 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"
android: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:id="@+id/cl"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

</android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>

<android.support.v4.widget.NestedScrollView
android:id="@+id/move_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:fillViewport="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/cl_parent">

<android.support.constraint.ConstraintLayout
android:id="@+id/move_list"
android:layout_width="match_parent"
android:layout_height="wrap_content">

</android.support.constraint.ConstraintLayout>
</android.support.v4.widget.NestedScrollView>

</android.support.constraint.ConstraintLayout>

这是我在应用程序中调用 xml 文件的地方,每次用户在棋盘上成功移动时都会执行此代码:

ConstraintLayout ml = findViewById(R.id.move_list);
TextView tv = new TextView(ChessBoardActivity.this);

float left = 34*density;
float top;
if(move == 'b') left += screenX/2.0;
top = (-30+30*turns)*density;

tv.setWidth((int)(screenX/2-34*density));
tv.setHeight((int)(30*density));
tv.setTranslationX(left);
tv.setTranslationY(top);
tv.setText(moveString);
ml.addView(tv);

任何解决此问题的帮助将不胜感激!谢谢:)

我希望当 TextView 超出屏幕但在 View 限制内时,nestedscrollview 可以滚动,但它根本不滚动。

最佳答案

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

旧答案:调用scrollView.invalidate(),以便 ScrollView 可以根据变化进行调整。如果它不起作用,那么也对 ConstraintLayout 调用 invalidate() 。如果仍然不起作用,请同时调用 requestLayout()

 ml.addView(tv);
ml.invalidate();
NestedScrollView scrollView = findViewById(R.id.move_view);
scrollView.invalidate();
scrollView.requestLayout();

关于java - 以编程方式添加 TextView 时 NestedScrollView 不滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57304226/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com