Android中TableLayout中的layout_span是什么意思?

7
我想要为Android应用程序创建一个登录界面。我使用了TableLayout来实现正确的对齐方式。因此,两行包括一个TextView和一个EditText,并且我想在它们下面添加一个宽度拉伸到屏幕的按钮。所以我把按钮放在另一个TableRow中,并在Button上添加layout_span="2",但是按钮显示在第一列。
我认为这应该是正确的,但我在xml文件中做错了什么。你有什么想法吗?
<TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".LoginActivity"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:paddingRight="15dp"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="@string/evUsername" />
        <EditText
            android:id="@+id/username"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:inputType="text" />
    </TableRow>
    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:paddingRight="15dp"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="@string/evPassword" />
        <EditText
            android:id="@+id/password"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:inputType="textPassword" />
    </TableRow>
    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <Button
            android:id="@+id/btnLogin"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_span="2"
            android:text="@string/btnLogin" />
    </TableRow>
</TableLayout>

提前感谢您!

2个回答

4
在您的原始XML文件中,您可以简单地在按钮中添加
android:layout_weight="1"
来实现布局权重。

3

最后,我将TableLayout包装在一个LinearLayout中,并在TableLayout之后添加了Button

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".LoginActivity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="5dp"
                android:paddingRight="15dp"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="@string/evUsername" />
            <EditText
                android:id="@+id/username"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:inputType="text" />
        </TableRow>
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="5dp"
                android:paddingRight="15dp"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="@string/evPassword" />
            <EditText
                android:id="@+id/password"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:inputType="textPassword" />
        </TableRow>
    </TableLayout>
    <Button
        android:id="@+id/btnLogin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/btnLogin" />
</LinearLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接