【问题标题】:Center and stretch button in TableRowTableRow 中的居中和拉伸按钮
【发布时间】:2021-12-11 04:56:42
【问题描述】:

我有这个代码

<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",但它不起作用

【问题讨论】:

    标签: android xml android-layout mobile


    【解决方案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,比如这些,或者甚至是非常宽容的约束布局。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-27
    • 1970-01-01
    • 2016-01-06
    • 2021-06-28
    • 1970-01-01
    • 2018-07-30
    相关资源
    最近更新 更多