我在这里遇到TableLayout的麻烦。我的目标是将两列的宽度相等,每列具有一幅图像,该图像应调整其宽度,以使其适合单元格的可用空间,但是似乎图像在单元格/行的可用空间之外不断溢出。到目前为止,我已经测试了一些没有运气的属性,因此可以在这里使用一些帮助。
这是设计视图中的当前结果:
我曾期望泰迪熊在左侧仅占据行宽的50%,然后另一个图像(这是一把雨伞)在熊的右侧占据其余50%。但是您会发现这不是正在发生的事情。
这是布局代码:
<?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:id="@+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".view.DebugFragment">
<TableLayout
android:id="@+id/tableImages"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:stretchColumns="*"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TableRow
android:layout_width="0dp"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
app:srcCompat="@drawable/teddy_bear" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
tools:srcCompat="@drawable/umbrella" />
</TableRow>
</TableLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
您需要使用android:shrinkColumns="*"
而不是android:strechColumns="*"
使所有列都可收缩,以便自动确定宽度以适合屏幕上的图像。拉伸它们只会使它们变大。