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

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

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属性是"要拉伸的列的从零开始的索引"。通过给它赋值*,我们说我们希望所有列均匀拉伸。通过调整布局权重,我们可以改变不同视图的大小。希望这对你有所帮助。在我看来,对于这些相对简单的UI,你应该考虑使用相对或线性布局,或者甚至是非常宽容的约束布局。

最新更新