Scrollview 不断垂直扩展更多的文本视图添加到其中
Scrollview keeps expanding vertically the more textviews are added into it
所以我有一个表格布局,我正在使用权重在屏幕上垂直拆分行。我动态地将 textviews 添加到第三行,但是当我这样做时,我添加的越多,scrollview 和 tablerow 垂直扩展得越多(即使我使用 weight 和 constraintTop 限制它...)
截图:
ui 没有文本视图:https://imgur.com/a/xxZ4Tyv
ui 带有一些文本视图:https://imgur.com/a/ago0r9U
ui 一大堆文本视图:https://imgur.com/a/GBO5d1Z(前两个表行完全被第三行覆盖)
我的目标是这样:https://imgur.com/a/6LsbG1I
这是我的 XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main">
<TableLayout
android:id="@+id/table"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:shrinkColumns="*"
android:stretchColumns="*"
android:weightSum="1">
<TableRow
android:id="@+id/UIDcontainer"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".2">
<TextView
android:id="@+id/UID"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="TextView" />
</TableRow>
<TableRow
android:id="@+id/HOWTOcontainer"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".2">
<TextView
android:id="@+id/HOWTO"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Approach your phone to the terminal"
android:textStyle="bold" />
</TableRow>
<TableRow
android:id="@+id/SCROLLVIEWcontainer"
android:layout_weight=".6"
android:layout_width="match_parent"
android:layout_height="0dp"
android:nestedScrollingEnabled="true">
<ScrollView
android:id="@+id/SCROLLVIEW"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="SpeakableTextPresentCheck">
<LinearLayout
android:id="@+id/SCROLL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</ScrollView>
</TableRow>
</TableLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
下面是动态添加textviews的代码:
val scroll = findViewById<View>(R.id.SCROLL) as LinearLayout
val tv = TextView(this)
tv.text = "This is a textview" //my own function would go here
tv.setPaddingRelative(0, 0, 0, 16)
tv.gravity = 1
scroll.addView(tv)
赋值时权重为importance
space
来自文档
This attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view.
您已赋予滚动视图更大的权重,因此随着您添加更多项目,它需要更多 space。
看起来您实际上是在尝试分配屏幕的百分比
所以像下面这样的东西应该可以满足您的需求,并且性能会更好,也更灵活
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main">
<TextView
android:id="@+id/UID"
android:layout_width="match_parent"
android:layout_height="0dp"
android:gravity="center"
android:text="TextView"
app:layout_constraintHeight_percent="0.2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/HOWTO"
android:layout_width="match_parent"
android:layout_height="0dp"
android:gravity="center"
android:text="Approach your phone to the terminal"
android:textStyle="bold"
app:layout_constraintHeight_percent="0.2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/UID"/>
<ScrollView
android:id="@+id/SCROLLVIEW"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintHeight_percent="0.6"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/HOWTO"
tools:ignore="SpeakableTextPresentCheck">
<LinearLayout
android:id="@+id/SCROLL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"/>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
制作中:-
所以我有一个表格布局,我正在使用权重在屏幕上垂直拆分行。我动态地将 textviews 添加到第三行,但是当我这样做时,我添加的越多,scrollview 和 tablerow 垂直扩展得越多(即使我使用 weight 和 constraintTop 限制它...)
截图:
ui 没有文本视图:https://imgur.com/a/xxZ4Tyv
ui 带有一些文本视图:https://imgur.com/a/ago0r9U
ui 一大堆文本视图:https://imgur.com/a/GBO5d1Z(前两个表行完全被第三行覆盖)
我的目标是这样:https://imgur.com/a/6LsbG1I
这是我的 XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main">
<TableLayout
android:id="@+id/table"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:shrinkColumns="*"
android:stretchColumns="*"
android:weightSum="1">
<TableRow
android:id="@+id/UIDcontainer"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".2">
<TextView
android:id="@+id/UID"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="TextView" />
</TableRow>
<TableRow
android:id="@+id/HOWTOcontainer"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".2">
<TextView
android:id="@+id/HOWTO"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Approach your phone to the terminal"
android:textStyle="bold" />
</TableRow>
<TableRow
android:id="@+id/SCROLLVIEWcontainer"
android:layout_weight=".6"
android:layout_width="match_parent"
android:layout_height="0dp"
android:nestedScrollingEnabled="true">
<ScrollView
android:id="@+id/SCROLLVIEW"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="SpeakableTextPresentCheck">
<LinearLayout
android:id="@+id/SCROLL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</ScrollView>
</TableRow>
</TableLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
下面是动态添加textviews的代码:
val scroll = findViewById<View>(R.id.SCROLL) as LinearLayout
val tv = TextView(this)
tv.text = "This is a textview" //my own function would go here
tv.setPaddingRelative(0, 0, 0, 16)
tv.gravity = 1
scroll.addView(tv)
赋值时权重为importance
space
来自文档
This attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view.
您已赋予滚动视图更大的权重,因此随着您添加更多项目,它需要更多 space。
看起来您实际上是在尝试分配屏幕的百分比
所以像下面这样的东西应该可以满足您的需求,并且性能会更好,也更灵活
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main">
<TextView
android:id="@+id/UID"
android:layout_width="match_parent"
android:layout_height="0dp"
android:gravity="center"
android:text="TextView"
app:layout_constraintHeight_percent="0.2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/HOWTO"
android:layout_width="match_parent"
android:layout_height="0dp"
android:gravity="center"
android:text="Approach your phone to the terminal"
android:textStyle="bold"
app:layout_constraintHeight_percent="0.2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/UID"/>
<ScrollView
android:id="@+id/SCROLLVIEW"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintHeight_percent="0.6"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/HOWTO"
tools:ignore="SpeakableTextPresentCheck">
<LinearLayout
android:id="@+id/SCROLL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"/>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
制作中:-