Android TableLayout:拉伸一列,两列宽度相等



我需要这样的东西,其中两列具有相同的宽度(0 和 2(,另一列拉伸以填充剩余空间(col 1(。但是,两个相等列的内容经常变化,因此有时第一列会比另一列大,反之亦然。我的第一个想法是使用TableLayout和android:layout_weight,但这并没有多大帮助,因为我为中间设置了android:layout_weight="1"android:layout_weight="2"

<TableRow
android:id="@+id/firstRow"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/0"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="0" />
<TextView
android:id="@+id/1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="2"
android:gravity="center"
android:text="1" />
<TextView
android:id="@+id/2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="2" />
</TableRow>

有没有办法仅使用布局或以编程方式执行此操作?

您可以使用app:layout_constraintHorizontal_weight属性将ContraintLayout与水平链一起使用。

<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/1"
android:layout_width="0dp"
android:layout_height="match_parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/2"
app:layout_constraintHorizontal_weight="2.5"/>
<TextView
android:id="@+id/2"
android:layout_width="0dp"
android:layout_height="match_parent"
app:layout_constraintRight_toRightOf="@+id/3"
app:layout_constraintRight_toLeftOf="@+id/1"
app:layout_constraintHorizontal_weight="5"/>
<TextView
android:id="@+id/3"
android:layout_width="0dp"
android:layout_height="match_parent"
app:layout_constraintLeft_toRightOf="@+id/2"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_weight="2.5"/>
</android.support.constraint.ConstraintLayout>

最新更新