TableRow 中的居中和拉伸按钮

Center and stretch button in TableRow

我有这个代码

<TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context=".MainActivity">

<TableRow>
    <TextView
            android:layout_weight="0.5"
            android:text="Login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="50dp"
            android:layout_marginStart="50dp"/>

    <EditText
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="25dp"
            android:layout_marginEnd="25dp"
    />
</TableRow>

<TableRow>
    <TextView
            android:layout_weight="0.5"
            android:text="Email"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="50dp"
            android:layout_marginStart="50dp"
    />

    <EditText
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="25dp"
            android:layout_marginEnd="25dp"
    />
</TableRow>

<TableRow>
    <Button
            android:text="Send"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
    />
</TableRow>

看起来像这样 image1

我需要将此按钮居中并拉伸到其他 table 行的大小,即授权表的大小。例如下面的截图

image2

我试过 android:stretchColumns="1" 但它不起作用

我查看了您的代码并能够通过以下方式创建您正在寻找的内容

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*"
android:gravity="center"
tools:context=".MainActivity">
<TableRow>
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="50dp"
        android:layout_marginLeft="50dp"
        android:layout_weight="0.3"
        android:text="Login" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="25dp"
        android:layout_marginRight="25dp"
        android:layout_weight="1" />
</TableRow>

<TableRow>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="50dp"
        android:layout_weight="0.3"
        android:text="Email" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="25dp"
        android:layout_marginRight="25dp"
        android:layout_weight="1" />
</TableRow>

<TableRow>
    <Button
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="50dp"
        android:layout_marginEnd="25dp"
        android:text="Send"
        android:layout_weight="1"/>
</TableRow>

如文档所示,stretchColumns 属性是“要拉伸的列的从零开始的索引”。通过给它值 * 我们说我们希望所有列均匀拉伸。通过调整布局权重,我们可以改变不同视图的大小。希望这对你有帮助。 IMO 你应该考虑使用相对或线性布局来实现相对简单的 UI,或者甚至是非常宽容的约束布局。