我的应用程序中的一个屏幕上包括棋盘和棋子,我想在其下方添加一个可滚动的视图,以在用户进行移动时显示移动符号。我可以将文本添加到视图中,但是无论我如何尝试,视图都不会滚动。
我已经广泛研究了其他用户在此问题上遇到的麻烦。我提供了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);
任何解决此问题的帮助将不胜感激!谢谢:)
我希望当textviews超出屏幕但在视图范围内时,nestedscrollview是可滚动的,但是它根本不会滚动。
答案 0 :(得分:0)
编辑:
我只是注意到代码中的实际问题。您正在使用平移手动设置位置,这样做会不更新父级布局的高度,这就是为什么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();